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

- Fix bundlers and file tracers packing the ESM copies of the standard font metrics instead of the CommonJS ones the Node build actually loads, which left `Cannot find module` errors for every standard font at runtime, by resolving the internal `#standard-fonts/*` mapping to a single file under all conditions
- Fix `doc.file()` throwing when the same in-memory attachment is embedded twice under one name, because the creation and modified dates the deduplication check compares are absent for sources that are not read from disk

### [v0.20.1] - 2026-08-23
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@
"node": "./lib/stream/node.js",
"default": "./lib/stream/browser.js"
},
"#standard-fonts/*": {
"require": "./js/standard-fonts/*.cjs",
"default": "./js/standard-fonts/*.mjs"
}
"#standard-fonts/*": "./js/standard-fonts/*.cjs"
},
"engine": [
"node >= v20.0.0"
Expand Down
24 changes: 24 additions & 0 deletions tests/package-resolution.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PDFDocument, { LineWrapper, registerFile } from 'pdfkit';
import { toBytes } from 'pdfkit/output';

const require = createRequire(import.meta.url);
const packageJson = require('../package.json');

assert.equal(
import.meta.resolve('pdfkit'),
Expand Down Expand Up @@ -40,6 +41,29 @@ assert.equal(
false,
);

// The node ESM build reaches the standard fonts through createRequire, so the
// require condition is the only one ever taken at runtime. Bundlers and file
// tracers walk this same file as ESM and resolve `#standard-fonts/*` under the
// import condition instead: if the two point at different files, the tracer packs
// the modules that are never loaded, omits the ones that are, and the bundle
// throws `Cannot find module` on the first document. Keep every standard font
// resolving to the one file the runtime uses, under both conditions.
const standardFonts = Object.keys(packageJson.exports)
.filter((entry) => entry.startsWith('./standard-fonts/'))
.map((entry) => entry.slice('./standard-fonts/'.length));

assert.equal(standardFonts.length, 14);

for (const font of standardFonts) {
const expected = new URL(`../js/standard-fonts/${font}.cjs`, import.meta.url)
.href;
assert.equal(import.meta.resolve(`#standard-fonts/${font}`), expected);
assert.equal(
require.resolve(`#standard-fonts/${font}`),
fileURLToPath(expected),
);
}

const fileDocument = new PDFDocument();
const fileOutput = toBytes(fileDocument);
fileDocument.font(
Expand Down
Loading