Skip to content

Add Share as PDF and Share Note File to share menu#1918

Open
gjouret wants to merge 1 commit intoglushchenko:masterfrom
gjouret:feature/share-as-pdf
Open

Add Share as PDF and Share Note File to share menu#1918
gjouret wants to merge 1 commit intoglushchenko:masterfrom
gjouret:feature/share-as-pdf

Conversation

@gjouret
Copy link
Copy Markdown

@gjouret gjouret commented Mar 20, 2026

Summary

  • Adds Share as PDF option to the share menu: renders the note to HTML, converts to a multi-page A4 PDF via WKWebView.createPDF(), and opens the system share sheet with the PDF file
  • Adds Share Note File option: shares the note's file/directory directly via the system share sheet (works for both plain .md files and .textbundle packages)
  • New PDFExporter helper class (follows the same WKWebView + WKNavigationDelegate pattern as the existing Printer.swift)
  • Fixes PDF export missing images: buildPage() previously skipped loadImages() when print: true, relying on assignBase64Images() which only matched standard ![alt](path) syntax — images inside markdown tables were missed. Removed the !print guard so loadImages() copies image files to the temp bundle for all export modes.

Implementation Details

  • PDF rendering uses MPreviewView.buildPage() to generate HTML, then loads it in a headless WKWebView
  • After content loads, JavaScript measures document.body.scrollHeight and the web view is resized to the full content height before calling createPDF() — this ensures multi-page documents render completely
  • The PDFExporter is retained via a static property on EditorViewController to prevent deallocation during async rendering
  • Both new options appear in the existing NSSharingServicePicker alongside Web, Copy Plain Text, and Copy HTML
  • loadImages() now runs for print/PDF export mode too — it copies image files from the note's assets/ directory to the temp bundle so the headless WKWebView can resolve relative <img src> paths
  • Requires macOS 11.0+ (same as existing share menu items)

Test plan

  • Select a note and click the share button — verify "Share as PDF" and "Share Note File" appear in the menu
  • Share as PDF on a short note — verify a single-page PDF is created and the share sheet opens
  • Share as PDF on a long note with multiple pages — verify all pages are included in the PDF
  • Share as PDF on a note with inline images — verify images appear in the exported PDF
  • Share as PDF on a note with a PDF thumbnail table — verify the thumbnail renders in the PDF
  • Share Note File on a .md note — verify the file URL is shared
  • Share Note File on a .textbundle note — verify the bundle directory is shared
  • Test from both the main window and a note opened in a separate window

🤖 Generated with Claude Code

Adds two new sharing options to the NSSharingServicePicker: - **Share as PDF**: Renders the note to HTML via MPreviewView.buildPage(), loads it in a headless WKWebView, measures full document height for multi-page support, then creates a PDF via WKWebView.createPDF(). The PDF is saved to a temp file and shared via the system share sheet. - **Share Note File**: Shares the note's file URL directly (works for both plain .md files and .textbundle packages) via the system share sheet. New file: PDFExporter.swift — async WKWebView-based PDF renderer, following the same pattern as the existing Printer.swift helper. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@glushchenko
Copy link
Copy Markdown
Owner

I just wanted to let you know that I'm on vacation right now; I'll look into this in mid-April. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants