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 @@ -8,6 +8,7 @@
- Add pageLayout option to control how pages are displayed in PDF viewers
- Fix Interlaced PNG with indexed transparency rendered incorrectly
- Preserve existing PageMode instead of overwriting when adding outlines
- Add userUnit option for custom page units (PDF 1.6)
- Support outlines that jump to specific page positions with custom zoom level

### [v0.17.2] - 2025-08-30
Expand Down
14 changes: 13 additions & 1 deletion docs/paper_sizes.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ In order to use the predefined sizes, the name of the size (as named in the list

// Passing size to the constructor
const doc = new PDFDocument({size: 'A7'});

// Passing size to the addPage function
doc.addPage({size: 'A7'});

### User Unit (PDF 1.6+)

The `userUnit` option allows you to scale the physical size of a page without changing the coordinate system. This is useful for creating large format documents like posters or banners that exceed the standard PDF coordinate limit (200 inches or 14400 points).

The default value is `1.0`, where 1 unit equals 1/72 inch (1 point). Setting `userUnit: 2.0` means 1 unit equals 2/72 inch, effectively doubling the physical size of the page.

// Create a page with 2x physical size
doc.addPage({ size: 'A4', userUnit: 2.0 });

// Create a large poster (4x scale)
doc.addPage({ size: [612, 792], userUnit: 4.0 });
2 changes: 2 additions & 0 deletions lib/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class PDFPage {
this._options = options;
this.size = options.size || 'letter';
this.layout = options.layout || 'portrait';
this.userUnit = options.userUnit || 1.0;

// calculate page dimensions
const dimensions = Array.isArray(this.size)
Expand Down Expand Up @@ -107,6 +108,7 @@ class PDFPage {
MediaBox: [0, 0, this.width, this.height],
Contents: this.content,
Resources: this.resources,
UserUnit: this.userUnit,
});

this.markings = [];
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/page.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import PDFDocument from '../../lib/document';

describe('page', function () {
describe('userUnit', function () {
test('defaults to 1.0', function () {
const doc = new PDFDocument();
expect(doc.page.userUnit).toBe(1.0);
expect(doc.page.dictionary.data.UserUnit).toBe(1.0);
});

test('can be set via page options', function () {
const doc = new PDFDocument({ autoFirstPage: false });
doc.addPage({ userUnit: 2.5 });
expect(doc.page.userUnit).toBe(2.5);
expect(doc.page.dictionary.data.UserUnit).toBe(2.5);
});
});

test('cascade page options', function () {
const doc = new PDFDocument({
autoFirstPage: false,
Expand Down
1 change: 1 addition & 0 deletions tests/unit/trailer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('Document trailer', () => {
/MediaBox [0 0 612 792]
/Contents 5 0 R
/Resources 6 0 R
/UserUnit 1
/Annots [9 0 R]
>>`,
]);
Expand Down