Skip to content

Fix PDF text misplacement/loss caused by mid-paragraph page breaks - #4

Merged
limsiokuan merged 2 commits into
mainfrom
copilot/fix-pdf-formatting-issue
Aug 4, 2026
Merged

Fix PDF text misplacement/loss caused by mid-paragraph page breaks#4
limsiokuan merged 2 commits into
mainfrom
copilot/fix-pdf-formatting-issue

Conversation

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Long markdown documents (e.g. demos.md) converted to PDFs with content missing or drawn in the wrong position — text would visually derail as soon as a paragraph wrapped across a page boundary, even though the same document previewed correctly in-app.

Root cause

  • drawRuns word-wraps text using a local cursor y1, calling ensureSpace(lineHeight) to trigger a page break when needed.
  • ensureSpace correctly reset the module-level y to margin on a new page, but y1 — the coordinate actually passed to doc.text(...) — was never updated, so subsequent words kept drawing at the stale, now off-page y1.

Fix

  • Replaced the boolean-returning ensureSpace(h) check inside the wrapping loop with ensureSpaceAt(yPos, h), which returns the correct y-coordinate to draw at (margin after a page break, unchanged otherwise) while still keeping the outer y in sync.
  • drawRuns now does y1 = ensureSpaceAt(y1, lineHeight) before each doc.text(...) call, so the wrapping cursor and page-break state can never diverge.
// before
if (ensureSpace(lineHeight)) { y1 = y; }
doc.text(w, x, y1);

// after
y1 = ensureSpaceAt(y1, lineHeight);
doc.text(w, x, y1);

No changes to parsing, block layout, or other renderers — this only corrects cursor tracking during inline word-wrapping across page boundaries.

Copilot AI linked an issue Aug 4, 2026 that may be closed by this pull request
Co-authored-by: limsiokuan <154960451+limsiokuan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incorrect formatting in PDF generation Fix PDF text misplacement/loss caused by mid-paragraph page breaks Aug 4, 2026
Copilot AI requested a review from limsiokuan August 4, 2026 08:03
@limsiokuan
limsiokuan marked this pull request as ready for review August 4, 2026 08:06
@limsiokuan
limsiokuan requested a lite review from Copilot August 4, 2026 08:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a jsPDF rendering bug where inline word-wrapping across a page boundary could cause text to be drawn at a stale Y coordinate, leading to missing/mispositioned content in long Markdown-to-PDF conversions.

Changes:

  • Updated ensureSpace to return a boolean when it performs a page break.
  • Added ensureSpaceAt(yPos, h) to page-break based on an explicit in-progress Y position used during wrapping.
  • Updated drawRuns to keep its wrapping cursor (y1) aligned with page breaks and to sync the outer y cursor after rendering.
Suppressed comments (1)

index.html:757

  • drawRuns now mutates the outer y as a side effect (y = y1 + lineHeight) even though every call site already assigns y = drawRuns(...). Keeping drawRuns side-effect free makes it easier to reason about cursor state and reduces the risk of future ordering bugs.
      y = y1 + lineHeight;
      return y;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread index.html
Comment on lines +721 to +726
// like ensureSpace, but operates on an explicit y position (e.g. the
// in-progress y1 used while wrapping lines inside drawRuns) instead of
// the outer `y`, and keeps the outer `y` in sync when a page break occurs
function ensureSpaceAt(yPos, h) {
if (yPos + h > pageH - margin) { doc.addPage(); y = margin; return margin; }
return yPos;
@limsiokuan
limsiokuan merged commit 2815418 into main Aug 4, 2026
2 checks passed
@limsiokuan
limsiokuan deleted the copilot/fix-pdf-formatting-issue branch August 4, 2026 08:16
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 is incorrectly formatted

3 participants