Skip to content

Fix PDF page dimensions for rendered documents#4

Open
lhr0909 wants to merge 1 commit into
sgade:mainfrom
lhr0909:fix/pdf-media-box-points
Open

Fix PDF page dimensions for rendered documents#4
lhr0909 wants to merge 1 commit into
sgade:mainfrom
lhr0909:fix/pdf-media-box-points

Conversation

@lhr0909
Copy link
Copy Markdown
Contributor

@lhr0909 lhr0909 commented Jun 3, 2026

Summary

  • use 72 PDF points per inch for the PDF media box instead of 300 DPI print dimensions
  • keep SwiftUI layout at display DPI, but separate layout-to-PDF scaling from print-quality rasterization scaling
  • add a Swift Testing regression test that renders an A4 PDF and verifies the media box is 595.28 × 841.89 points
  • run swift test in CI so the regression test is exercised on pull requests

Findings

While investigating #3, I found that PDFRenderer was using pageSize.size(atDPI: .print) for the Core Graphics PDF media box. Since .print is 300 DPI, A4 becomes 2480.31 × 3507.87 PDF units. PDF viewers interpret media box units as PDF points where 1 inch = 72 points, so the physical page is shown as roughly 875 × 1237.5 mm — close to A0.

Changing only the view/display DPI does not affect that physical page size because the oversized media box is created before rendering. The fix is to use 72-point PDF units for the page box and keep print DPI as a separate rasterization-quality concern.

Fixes #3.

Validation

swift test --configuration release

@lhr0909 lhr0909 force-pushed the fix/pdf-media-box-points branch from 4b00c01 to db59ef2 Compare June 3, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How do I create PDFs at the right page size?

1 participant