Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
7 changes: 7 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
84 changes: 84 additions & 0 deletions tests/unit/document.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
Loading