Node build can't render once bundled: pdfkit reads its standard fonts from disk next to its own module
Describe the bug
Since 4.9.0 (upstream pdfkit 0.20.1 instead of @react-pdf/pdfkit), a Node app that bundles react-pdf into one file, e.g. a Lambda handler built with esbuild, can't render anything.
pdfkit's Node build registers loaders for its standard fonts that require() a data file relative to pdfkit's own built module. Inlined into a bundle, that path points next to the bundle, the file isn't there, and the first doc.font('Helvetica') throws. @react-pdf/font hits this on every document: the FontStore constructor preloads four Helvetica faces on import, and openStandardFont creates a PDFDocument, which opens Helvetica in initFonts whatever face was asked for.
The browser build is fine because it imports all fonts from pdfkit/standard-fonts/* and calls registerStdFonts() up front. The Node build doesn't, per the comment in standard-font.ts that "the node build registers them itself". pdfkit does, but only while it still lives in its own package directory. #3550 is the same root cause seen from a file tracer instead of a bundler.
Two things make it worse than a missing font:
- The four preloads in the
FontStoreconstructor aren't awaited or caught. A failed one is an unhandled rejection at import time, which kills a Node process with default settings, and the rejected promise stays cached on theFontSource(#3568 addresses that part), so a laterFont.load()can't recover. - Bundled as CommonJS, pdfkit 0.20.1 throws
Invalid URLon import, before any font is touched. That's pdfkit's side, see foliojs/pdfkit#1802.
To Reproduce
// probe.mjs
import { Document, Page, Text, renderToBuffer } from '@react-pdf/renderer';
import { createElement } from 'react';
renderToBuffer(
createElement(Document, null,
createElement(Page, null,
createElement(Text, null, 'hello'))),
).then((pdf) => console.log(pdf.subarray(0, 5).toString()));npm i @react-pdf/[email protected] react esbuild
node probe.mjs # %PDF-
npx esbuild probe.mjs --bundle --platform=node --format=esm --outfile=out.mjs \
--banner:js='import{createRequire as __createRequire}from"node:module";const require=__createRequire(import.meta.url);'
node out.mjs # Error: Cannot find module '#standard-fonts/Helvetica'
npx esbuild probe.mjs --bundle --platform=node --format=cjs --outfile=out.cjs
node out.cjs # TypeError: Invalid URLThe ESM error is thrown from pdfkit's getStandardFont, reached through PDFDocument.initFonts → new PDFDocument → openStandardFont → new StandardFont → FontSource._load.
Expected behavior
A bundled react-pdf renders the same document as an unbundled one. Registering all fourteen fonts in the Node build would fix it but undo the lazy loading pdfkit added on purpose in 0.18. FontSource.load() is already async, though, so the Node build can import('pdfkit/standard-fonts/<Name>') on first use and pass the data to registerStdFonts(). Every face stays lazy, it works bundled or not, and both builds share one code path. It needs pdfkit's Node build to export registerStdFonts, which foliojs/pdfkit#1802 adds. Until that ships, marking pdfkit as external and installing it next to the bundle works, which is what we do in production right now. PR coming.
Separately, the constructor preloads shouldn't be able to crash the process, and a failed source should be retried on the next load() instead of returning the cached rejection.
Environment
- OS: macOS 15 and Amazon Linux 2023 (Lambda, Node 22)
- Browser: none, Node only
@react-pdf/renderer4.9.0,@react-pdf/font4.1.2, pdfkit 0.20.1, esbuild 0.28.2
This report was researched and written with Claude Code, an AI assistant, which also ran the reproduction above.
Source: diegomura/react-pdf