Skip to content

fix(pdf): fix page breaks and element scaling for PDF export (fixes #166)#168

Merged
ThisIs-Developer merged 11 commits into
mainfrom
codex/fix-pdf-page-breaks-166
Jun 10, 2026
Merged

fix(pdf): fix page breaks and element scaling for PDF export (fixes #166)#168
ThisIs-Developer merged 11 commits into
mainfrom
codex/fix-pdf-page-breaks-166

Conversation

@ThisIs-Developer

@ThisIs-Developer ThisIs-Developer commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Description

This PR resolves the issue where text, lists, table rows, and MathJax formulas are cut or sliced horizontally in half across A4 page boundaries during PDF export (Fixes #166).

Key Improvements

1. Expanded Page-Break Detection

  • Updated identifyGraphicElements to include page-break sensitive text elements (p, li, blockquote, h1-h6, hr) and MathJax display containers (mjx-container[display="true"], .math-block). These are labeled as 'text' or 'math' to prevent boundary slicing.

2. Dynamic Shift Offset Tracking

  • Implemented an accumulatedShift tracking parameter inside insertPageBreaks. As element margins are shifted down to begin at a new page boundary, subsequent element positions are dynamically offset to prevent coordinate overlap calculations from using stale values.

3. Table Splitting & Header Replication

  • Added helper functions splitTables and mergeSplitTables which divide tables exceeding a page's height into separate parts.
  • Clones and repeats the original thead headers on subsequent pages for continuous reading.
  • Inserts precise spacer elements to shift subsequent table rows to the top of the next page instead of letting them cross A4 boundaries.

4. Cascade Stabilization

  • Moved the call to handleOversizedElements inside the main applyPageBreaksWithCascade stabilization loop. Margins and scales are now resolved iteratively to guarantee layout stability.

5. Height Slicing & Box Sizing Fixes

  • Replaced tempElement.scrollHeight with Math.ceil(tempElement.getBoundingClientRect().height) in html2canvas window properties to handle transformed dimension heights correctly, eliminating empty trailing pages.
  • Corrected double-counting of padding in fitExportElementToContent and fitMarkdownExportToContent.
  • Cleaned up top-level YAML frontmatter parsing regex (/^\s*---/) so that newlines/whitespace preceding frontmatter do not break parsing.

Verification Results

Verified the changes locally using Playwright:

  • Lazy loads PDF libraries (jspdf and html2canvas) dynamically.
  • Pushes and offsets layout elements cleanly at page boundaries.
  • Compiles the PDF and saves the document successfully.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@vercel

vercel Bot commented Jun 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
markdown-viwer Ready Ready Preview, Comment Jun 10, 2026 5:59am

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 9, 2026

Copy link
Copy Markdown

Deploying markdown-viewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: 70915f6
Status: ✅  Deploy successful!
Preview URL: https://3fc26e46.markdown-viewer.pages.dev
Branch Preview URL: https://codex-fix-pdf-page-breaks-16.markdown-viewer.pages.dev

View logs

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 9, 2026

Copy link
Copy Markdown

Deploying markdownviewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: 70915f6
Status: ✅  Deploy successful!
Preview URL: https://cb2bfc07.markdownviewer.pages.dev
Branch Preview URL: https://codex-fix-pdf-page-breaks-16.markdownviewer.pages.dev

View logs

…SVGs/images and resetting transient styles in cascade
…itioning, and parent block redirection for inline images
…ia safety buffer and valid list item shifting
@ThisIs-Developer ThisIs-Developer merged commit 03c7bdb into main Jun 10, 2026
8 checks passed
@ThisIs-Developer ThisIs-Developer deleted the codex/fix-pdf-page-breaks-166 branch June 10, 2026 12:41
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.

PDF export : page break problem

1 participant