feat: accept pre-parsed fontkit Font instances in doc.font() - #1776
Merged
Conversation
diegomura
force-pushed
the
diegomura/pdfkit-font-object-support
branch
from
August 23, 2026 11:42
c1e4003 to
7a7fd23
Compare
diegomura
force-pushed
the
diegomura/pdfkit-font-object-support
branch
from
August 23, 2026 11:46
7a7fd23 to
9733b3a
Compare
diegomura
requested review from
blikblum and
devongovett
and
a lite review from Copilot
August 23, 2026 11:46
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends PDFKit’s font-loading pipeline so doc.font() / registerFont() can accept an already-parsed fontkit Font instance (in addition to file paths and raw byte sources), avoiding redundant parsing when callers already use fontkit for layout (e.g., react-pdf).
Changes:
- Update
PDFFontFactory.open()to accept pre-parsed fontkitFontinstances (duck-typed vialayout()). - Add unit tests covering embedding, caching behavior, non-collision, and
registerFont()with parsed instances. - Document the new supported input type and note it in the changelog.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/unit/font.spec.js | Adds tests for passing parsed fontkit Font instances into doc.font() and registerFont(). |
| lib/font_factory.js | Accepts pre-parsed fontkit Font objects in PDFFontFactory.open(). |
| docs/text.md | Documents support for fontkit Font instances as font() inputs. |
| CHANGELOG.md | Notes the new support in the Unreleased section. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+75
to
+78
| const pdf = collectPdf(doc); | ||
| expect(missingObjects(pdf)).toHaveLength(0); | ||
| }); | ||
| }); |
diegomura
force-pushed
the
diegomura/pdfkit-font-object-support
branch
from
August 23, 2026 12:35
8bd4daa to
9733b3a
Compare
blikblum
approved these changes
Aug 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
PDFFontFactory.opennow accepts an already-parsed fontkitFontinstance (duck-typed viasrc.layout) in addition to string paths,Uint8Array, andArrayBuffer. Unsupported objects still hit the existing "Not a supported font format" error.Why
react-pdf maintains a fork of pdfkit (
@react-pdf/pdfkit) and is converging back onto upstream. Nearly all fork deltas have been upstreamed already; this is one of the last functional gaps. react-pdf does its own text layout with fontkit and passes the parsedFontinstance todoc.font()so pdfkit doesn't re-parse bytes it already has in memory.The existing cache machinery in
lib/mixins/fonts.js(nullcacheKeyfor non-string sources,isEqualFontdedup) already handles these sources — no changes needed there; tests cover that path.Tests
doc.font(fontInstance)embeds the font and renders textEmbeddedFontregisterFont('MyFont', fontInstance)+doc.font('MyFont')works