PDFKit crashes when providing fonts as Buffer in table
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- javascript
- Domain
- backend
Research direction
Start in lib/table/normalize.js at normalizeCell(), especially the deep merge linked in the report, and reproduce the issue with a Buffer font in a table cell using the supplied sample. The fix is complete when the font source remains usable rather than becoming an object with numeric keys, and the sample no longer raises the unsupported-font error.
Written by the indexing model from the issue text.
Description
Bug Report
Description of the problem
When providing font in table cells with a src of Buffer or UintArray, Not a supported font format or standard PDF font. will be thrown.
Upon some digging, I found that in normalizeCell() the font object is deep-merged with other fonts in table styles
This line took an unusually long time to execute, and after this the font src became something like
console.log(font.src)
// { '1': number, '2': number, '3': number, ... }
Code sample
import PDFDocument from "pdfkit";
import fs from "fs";
const doc = new PDFDocument();
doc.table().row([
{
text: 'Hello World',
font: { src: fs.readFileSync(SOME_CUSTOM_FONT), family: FONT_FAMILY },
}
])
Possible fix
Replace the font deep merge with manual property assignment.
Considering the structure of the font object a shallow merge may also be enough.
// changing to this worked properly in my case
const font = {...colStyle.font, ...rowStyle.font, ...cell.font}
Your environment
- pdfkit version: 0.19.1
- Node version: 24.15.0
- Browser version (if applicable): N/A
- Operating System: Windows 11
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 1.2k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 18
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from foliojs/pdfkit
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Upgrade codemirror Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·