Skip to content

Keep wrapped text at mirrored page margins - #1889

Open
clean99 wants to merge 1 commit into
foliojs:masterfrom
clean99:contrib/pdfkit-1057-wrapped-text-page-margins
Open

clean99 wants to merge 1 commit into
foliojs:masterfrom
clean99:contrib/pdfkit-1057-wrapped-text-page-margins

Conversation

@clean99

@clean99 clean99 commented Dec 1, 2026

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix. Wrapped text keeps the previous page's x when a newly added page has mirrored margins. Fixes #1057, following the conditional check requested in #436.

  • Problem: with left/right margins alternating between 60/20 and 20/60, text on page 2 still starts at 60 and can cross its right margin.
  • Change: save the old left margin before adding the page, then update startX only if it matched that margin. Width and page-option inheritance stay unchanged; no new event or option.
  • Verification: npm exec -- vitest run tests/unit/text.spec.js checks emitted PDF positions over repeated page breaks, a non-margin x, and explicit width. Three cases fail without the source fix. All 699 unit/visual tests pass on Node 20, 22, and 24 with npm test -- --maxWorkers=2; default concurrency hit a visual-test timeout on both base and head. Lint, formatting, build, package checks, and 53 tool tests pass.
Reproduction on master 89d5965

After yarn install and yarn build, run this from the checkout. Page 2 starts at x=60 before the fix and x=20 afterward.

node <<'JS'
const PDFDocument = require('pdfkit');
const doc = new PDFDocument({
  size: [300, 200],
  margins: { top: 20, bottom: 20, left: 60, right: 20 }
});
doc.pipe(require('fs').createWriteStream('mirrored.pdf'));
let page = 1;
doc.on('pageAdded', () => {
  const even = ++page % 2 === 0;
  doc.page.margins.left = even ? 20 : 60;
  doc.page.margins.right = even ? 60 : 20;
  doc.x = doc.page.margins.left;
});
doc.text('word '.repeat(240));
doc.end();
JS

Checklist:

  • Unit Tests
  • Documentation — N/A; existing margin behavior, no new API
  • Update CHANGELOG.md
  • Ready to be merged

This branch has not been deployed

No deployments
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.

Inside and outside margins for binding / book printing

1 participant