Fix PDF text misplacement/loss caused by mid-paragraph page breaks - #4
Merged
Conversation
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
limsiokuan
marked this pull request as ready for review
August 4, 2026 08:06
There was a problem hiding this comment.
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
ensureSpaceto 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
drawRunsto keep its wrapping cursor (y1) aligned with page breaks and to sync the outerycursor after rendering.
Suppressed comments (1)
index.html:757
drawRunsnow mutates the outeryas a side effect (y = y1 + lineHeight) even though every call site already assignsy = drawRuns(...). KeepingdrawRunsside-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 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; |
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.
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
drawRunsword-wraps text using a local cursory1, callingensureSpace(lineHeight)to trigger a page break when needed.ensureSpacecorrectly reset the module-levelytomarginon a new page, buty1— the coordinate actually passed todoc.text(...)— was never updated, so subsequent words kept drawing at the stale, now off-pagey1.Fix
ensureSpace(h)check inside the wrapping loop withensureSpaceAt(yPos, h), which returns the correct y-coordinate to draw at (marginafter a page break, unchanged otherwise) while still keeping the outeryin sync.drawRunsnow doesy1 = ensureSpaceAt(y1, lineHeight)before eachdoc.text(...)call, so the wrapping cursor and page-break state can never diverge.No changes to parsing, block layout, or other renderers — this only corrects cursor tracking during inline word-wrapping across page boundaries.