/ tools / mdshare
Sign in
tools / mdshare

Export to PDF via the browser print pipeline #1

Back to pull requests
Mergedowner wants to merge 0 commits into main ← pdf-export
Merged
owner merged this as 1be0f48
owneropened this

Sharing a link covers most readers, but a document sometimes has to become a file: an attachment, a printed handout, something to archive. This adds an Export PDF button that prints the rendered pane through the browser's own "Save as PDF" destination. Because the export is a print of the live DOM rather than a rasterized snapshot, math stays selectable text and mermaid diagrams stay vector SVG, and no rasterizing library or compile server is involved. Ctrl/Cmd+P picks up the same stylesheet.

src/print.css turns the two-pane layout into a single flowing document: the toolbar, splitter, editor pane and code copy-buttons are hidden, and the 100vh/overflow constraints that make the screen version scroll are lifted, since otherwise the print is clipped to a single page. Page breaks are kept out of equations, diagrams, images and table rows; a table continued on the next page repeats its header row; long code blocks and long tables are allowed to break across pages, because forbidding it mostly leaves blank paper. Code and table shading is marked print-color-adjust: exact so it survives a dialog with background graphics switched off, and pre wraps rather than being cut at the right margin.

src/title.ts derives the document's name from its first ATX heading, skipping fenced code blocks so that a # comment inside a python block is not mistaken for a heading. That name becomes the tab title, and is used bare during printing so the browser suggests a sensible filename.

Verified with headless Chrome on two documents. The sample document prints over two pages with centered display math, shaded highlighted code, the GFM table, task list, mermaid diagram and footnotes all intact. A synthetic document breaks a 34-row table across a page boundary with its header repeated, and a 60-line code block across two pages, with no blank gaps and nothing clipped. npm run build and npm run lint are clean.

Two limitations worth recording. Page numbers and the date-and-URL header come from the browser's print dialog rather than from the page, since Chrome and Firefox do not support @page margin boxes. And there is no equivalent of \newpage: because mdshare deliberately renders raw HTML as text, a forced page break would need a small remark plugin recognizing a marker such as a lone \pagebreak paragraph.

Sign in to comment.

0 commits

Nothing to merge.

Files changed

No changes.

↑↓ move↵openescclose