fix: preserve reading order for styled inline text in doc.html() (#4000) - #4015
Open
Praddy2006 wants to merge 1 commit into
Open
fix: preserve reading order for styled inline text in doc.html() (#4000)#4015Praddy2006 wants to merge 1 commit into
Praddy2006 wants to merge 1 commit into
Conversation
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.
Description
Fixes #4000 — text extracted from PDFs generated via
doc.html()couldappear out of reading order when the source HTML mixed plain text with
styled inline elements (
<strong>,<em>,<span>, etc.). This happenedbecause 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: WhenautoPaging === 'text', text draws arenow buffered (
textRunBuffer) instead of written immediately. A newflushTextRuns()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 afterhtml2canvas rendering completes in
toContext2d_post().'slice'/trueautoPaging mode is untouched — bufferingonly applies to
'text'mode, since 'slice' mode isn't affected by thisissue and direct Context2D API callers may not always route through
doc.html()'s flush hook.Testing
test/specs/html-text-order.spec.jswith two cases:<strong>/<em>inline runs<span>-split, nested inline, whole-phrase bold), asserting theyextract in correct top-to-bottom order
npm run test-local— 467 specs, 0 failuresChecklist
npm run prettierrun before commitdist/files included in this PR