From 2318324e55e08bc6a20a8f608ebd4bee0ad0632d Mon Sep 17 00:00:00 2001 From: Terry <186405119+tlq5l@users.noreply.github.com> Date: Sun, 22 Nov 2026 04:05:39 +0700 Subject: [PATCH] Add onPageAdded constructor option The pageAdded event is emitted inside addPage(), and the constructor adds the first page before a caller can attach a listener. A page added that way could not be written to with on('pageAdded'). The new onPageAdded option takes a function, registers it before the automatic first page is added, and calls it with the document for every page. A first page added by hand with autoFirstPage: false is covered too. The emission point of pageAdded does not change. Listeners attached with on() or once() see the same events in the same order as before, and no event methods are overridden. Fixes #1529 Agent: claude --- CHANGELOG.md | 1 + docs/getting_started.md | 8 +++- lib/document.js | 7 ++++ tests/unit/document.spec.js | 84 +++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86d0a233e..5cf2b3cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Add an `onPageAdded` document option, a function called with the document each time a page is added, including the automatic first page that a `pageAdded` listener attached after construction would miss. Fixes #1529 - Fix smooth curves (`S`, `T`) in SVG paths reflecting the control point of an arc or of a curve of the other kind. As the SVG spec says, they now start from the current point there. An arc with a zero radius is now drawn as a straight line instead of nothing ### [v0.21.0] - 2026-11-19 diff --git a/docs/getting_started.md b/docs/getting_started.md index 903bc0738..0a6a1befb 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -189,7 +189,13 @@ quite simple! doc.addPage() -To add some content every time a page is created, either by calling `addPage()` or automatically, you can use the `pageAdded` event. +To add some content every time a page is created, either by calling `addPage()` or automatically, you can pass an `onPageAdded` function when creating the document. It is called synchronously each time a page is added and receives the document as its argument, so it can draw on the automatic first page that is added before construction returns (or on the first `addPage()` call when `autoFirstPage: false` is used). + + const doc = new PDFDocument({ + onPageAdded: doc => doc.text("Page Title") + }); + +For pages added after construction, you can also use the `pageAdded` event directly. doc.on('pageAdded', () => doc.text("Page Title")); diff --git a/lib/document.js b/lib/document.js index c5a35d19d..0a93c9768 100644 --- a/lib/document.js +++ b/lib/document.js @@ -141,6 +141,13 @@ class PDFDocument extends Readable { // 4 binary chars, as recommended by the spec this._write('%\xFF\xFF\xFF\xFF'); + // The onPageAdded option runs on every page, including the first one, + // which is added before a listener could be attached after construction + if (typeof this.options.onPageAdded === 'function') { + const { onPageAdded } = this.options; + this.on('pageAdded', () => onPageAdded(this)); + } + // Add the first page if (this.options.autoFirstPage !== false) { this.addPage(); diff --git a/tests/unit/document.spec.js b/tests/unit/document.spec.js index a7d7d4bc9..81f220a7a 100644 --- a/tests/unit/document.spec.js +++ b/tests/unit/document.spec.js @@ -1,6 +1,7 @@ import { vi } from 'vitest'; import PDFDocument from '../../lib/document'; import { logData } from './helpers'; +import Helvetica from '../../lib/font/generated/Helvetica'; describe('PDFDocument', () => { describe('font option', () => { @@ -92,4 +93,87 @@ describe('PDFDocument', () => { expect(doc._root.data.PageLayout).toBe(expected); }); }); + + describe('onPageAdded option', () => { + test.each([{}, { autoFirstPage: true }])( + 'runs once during construction and once per addPage, options %j', + (options) => { + let pageNumber = 0; + const onPageAdded = vi.fn((d) => { + pageNumber += 1; + d.text(`page ${pageNumber} text`); + }); + const doc = new PDFDocument({ + bufferPages: true, + compress: false, + ...options, + onPageAdded, + }); + const data = logData(doc); + + expect(onPageAdded).toHaveBeenCalledTimes(1); + expect(onPageAdded).toHaveBeenLastCalledWith(doc); + + doc.addPage(); + expect(onPageAdded).toHaveBeenCalledTimes(2); + expect(onPageAdded).toHaveBeenLastCalledWith(doc); + + doc.end(); + expect(data).toContainText({ text: 'page 1 text' }); + expect(data).toContainText({ text: 'page 2 text' }); + }, + ); + + test('runs on the first manual addPage with autoFirstPage: false', () => { + const onPageAdded = vi.fn(); + const doc = new PDFDocument({ autoFirstPage: false, onPageAdded }); + + expect(onPageAdded).not.toHaveBeenCalled(); + + doc.addPage(); + expect(onPageAdded).toHaveBeenCalledTimes(1); + expect(onPageAdded).toHaveBeenLastCalledWith(doc); + doc.end(); + }); + + test('receives the document on the browser stream', async () => { + vi.resetModules(); + vi.doMock('#stream', () => import('../../lib/stream/browser.js')); + try { + const standardFonts = await import('../../lib/font/standard_fonts'); + standardFonts.registerStdFonts(Helvetica); + const { default: BrowserPDFDocument } = + await import('../../lib/document'); + + const onPageAdded = vi.fn((d) => d.text('browser first page')); + const doc = new BrowserPDFDocument({ compress: false, onPageAdded }); + const data = logData(doc); + + expect(onPageAdded).toHaveBeenCalledTimes(1); + expect(onPageAdded).toHaveBeenLastCalledWith(doc); + + doc.end(); + expect(data).toContainText({ text: 'browser first page' }); + } finally { + vi.doUnmock('#stream'); + vi.resetModules(); + } + }); + + test('does not change listeners attached after construction', () => { + const doc = new PDFDocument(); + const trace = []; + doc.on('pageAdded', () => trace.push('on')); + doc.once('pageAdded', () => trace.push('once')); + + expect(trace).toEqual([]); + + doc.addPage(); + expect(trace).toEqual(['on', 'once']); + + doc.addPage(); + expect(trace).toEqual(['on', 'once', 'on']); + doc.end(); + }); + }); });