Skip to content

Emit pageAdded for the first page when autoFirstPage is on - #1870

Closed
tlq5l wants to merge 1 commit into
foliojs:masterfrom
tlq5l:fix/pageadded-first-page
Closed

tlq5l wants to merge 1 commit into
foliojs:masterfrom
tlq5l:fix/pageadded-first-page

Conversation

@tlq5l

@tlq5l tlq5l commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix. Fixes #1529.

With default autoFirstPage, the constructor adds page 1 before listeners attach, so pageAdded is missed:

const doc = new PDFDocument();
doc.on('pageAdded', () => {
  // Missed for the first page before this fix
});

The constructor now suppresses and holds that pageAdded event, delivering it synchronously (page 1 current) to the first listener attached via on, once, addListener, prependListener, or prependOnceListener. On the browser build, addListener, prependListener, and prependOnceListener are overridden only if defined on the base stream, preserving feature-detection fallback to on(). If addPage() or end() runs first, the held event is dropped. With no listener or autoFirstPage: false, behavior is unchanged.

Behavior change: a pageAdded listener attached before another page now runs once synchronously inside on() (or other attach methods). Callers assuming it waits for the next addPage() see one extra call.

Tests: 7 unit tests added in tests/unit/document.spec.js. Ran on a box: yarn install --immutable && yarn vitest run tests/unit (616 of 616 passed in 47 files; exit 0), yarn install --immutable && yarn vitest run tests/unit/document.spec.js (22 of 22 passed; exit 0; with lib/document.js reverted to master, 5 of the 7 new tests fail; exit 1), yarn lint (exit 0), yarn prettier --check lib tests (exit 0), and yarn build (exit 0).

Checklist:

  • Unit Tests
  • Documentation N/A
  • Update CHANGELOG.md N/A
  • Ready to be merged

Written with AI assistance.

The first-page pageAdded fix overrode addListener, prependListener and prependOnceListener on every build. The browser build's event base has on() and once() only, so code that checks typeof addListener and then calls it threw, where master fell back to on().

These overrides are now added only for the listener methods the base class really has. The browser build gets on() and once() overrides only, so feature detection falls back to on() as it does on master.

Agent: claude
@blikblum

Copy link
Copy Markdown
Member

Not a clean solution. Better either an on parameter in constructor that attaches events or a onPageAdded constructor param. Elaborate tradeoffs and propose a new PR

@blikblum blikblum closed this Nov 21, 2026
@tlq5l tlq5l mentioned this pull request Nov 22, 2026
4 tasks
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.

pageAdded event not called for first page with autoFirstPage: true

2 participants