Skip to content

fix: preserve reading order for styled inline text in doc.html() (#4000) - #4015

Open
Praddy2006 wants to merge 1 commit into
parallax:masterfrom
Praddy2006:fix/html-text-order-4000
Open

fix: preserve reading order for styled inline text in doc.html() (#4000)#4015
Praddy2006 wants to merge 1 commit into
parallax:masterfrom
Praddy2006:fix/html-text-order-4000

Conversation

@Praddy2006

Copy link
Copy Markdown

Description

Fixes #4000 — text extracted from PDFs generated via doc.html() could
appear out of reading order when the source HTML mixed plain text with
styled inline elements (<strong>, <em>, <span>, etc.). This happened
because html2canvas paints DOM nodes in CSS painting order, which does not
always match DOM/reading order, and jsPDF wrote each text draw to the PDF
content stream as it was painted.

Changes

  • src/modules/context2d.js: When autoPaging === 'text', text draws are
    now buffered (textRunBuffer) instead of written immediately. A new
    flushTextRuns() method sorts buffered entries into top-to-bottom,
    left-to-right reading order (grouping by line via a y-tolerance) before
    writing them to the content stream.
  • src/modules/html.js: flushTextRuns() is called automatically after
    html2canvas rendering completes in toContext2d_post().
  • The existing 'slice'/true autoPaging mode is untouched — buffering
    only applies to 'text' mode, since 'slice' mode isn't affected by this
    issue and direct Context2D API callers may not always route through
    doc.html()'s flush hook.

Testing

  • Added test/specs/html-text-order.spec.js with two cases:
    • a single sentence containing <strong>/<em> inline runs
    • four paragraphs exercising different mixed-styling patterns (plain,
      <span>-split, nested inline, whole-phrase bold), asserting they
      extract in correct top-to-bottom order
  • npm run test-local — 467 specs, 0 failures

Checklist

  • Test cases added
  • npm run prettier run before commit
  • No dist/ files included in this PR
  • No unrelated changes

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.

Merged words in pdf after saving

1 participant