fix: PDF image spacing, page top margin, and soft line breaks#59
Merged
Conversation
Three interrelated PDF layout fixes: - Page top margin: own physical margins via CSS @page with preferCSSPageSize, and zero printToPDF margins. Non-zero printToPDF margins make Chromium use them for the page box, which drops the top margin to 0 on continuation pages (images stuck to page top). - Image spacing: Obsidian merges consecutive image embeds (and adjacent text) into one <p> separated by <br>. Broaden the media-paragraph spacing collapse from :has(> img:only-child) to descendant :has(img) so multi-media paragraphs tighten. Control media gap via line-height:1 plus per-media margin. - Soft line breaks: do NOT hide <br> in media paragraphs. A prior rule (p:has(img) > br { display:none}) collapsed all <br>, which silently merged soft line breaks when text and media shared one <p>. Add a regression test forbidding display:none on <br> anywhere in the PDF stylesheet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Exported PDFs had three layout defects, all stemming from how CSS interacts with Obsidian's rendered DOM:
Root Causes & Fixes
Page top margin
Own physical margins via CSS
@pagewithpreferCSSPageSize: true, and zero theprintToPDFmargins. When both@pagemargin andprintToPDFmargins are non-zero, Chromium uses theprintToPDFmargins for the page box — and those drop the top margin to 0 on continuation pages. Verified via CDPPage.printToPDF(same path as Electron) + PyMuPDF coordinate measurement: withprintToPDFmargins at 0 and@page margin: 52px 60px, continuation-page top margin is correctly ~39pt.Image spacing
Obsidian merges consecutive image embeds (and adjacent text) into a single
<p>separated by<br>. Broadened the media-paragraph spacing collapse from:has(> img:only-child)to descendant:has(img)so multi-media paragraphs tighten. Media gap is controlled byline-height: 1(shrinks the<br>line-box) plus per-mediamargin.Soft line breaks
A prior rule
p:has(img) > br { display:none }— meant to tighten media spacing — collapsed all<br>in media paragraphs, silently merging legitimate soft line breaks when text and media shared one<p>. Removed the rule entirely. Added a regression test forbiddingdisplay:noneon<br>anywhere in the PDF stylesheet.Verification