diff --git a/CHANGELOG.md b/CHANGELOG.md index 86d0a233..5cf2b3cd 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 903bc073..0a6a1bef 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 c5a35d19..0a93c976 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 a7d7d4bc..81f220a7 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(); + }); + }); });