I am attempting to add a custom font to the pdf-lib library, but I am encountering an error: 'Unknown font format.' I have tried TTF, OTF, WOFF, and WOFF2 fromats, but none of them seem to work. Could someone please help me identify what I might be doing wrong in this process?
const url = "./Roboto-Italic.ttf";
const fontBytes = await fetch(url).then((res) => res.arrayBuffer());
pdfDoc.registerFontkit(fontkit);
const font = await pdfDoc.embedFont(fontBytes);
error:
at Object.create (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:8806:11)
at Function.<anonymous> (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:47584:42)
at step (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:56459:17)
at Object.next (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:56408:14)
at http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:56382:67
at new Promise (<anonymous>)
at __awaiter (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:56364:10)
at CustomFontEmbedder.for (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:47579:60)
at PDFDocument.<anonymous> (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:35659:91)
at step (http://localhost:3000/main.86466a5e8eefe1529961.hot-update.js:56459:17)
Most PDF libraries will use a TTF font for PDF mapping "ToUnicode". but there are security restrictions on imbedding binary fonts from external sources into a text based carrier like a PDF wrapper.
PDF-LIB uses a fontkit which needs to do the necessary embedding of pulling a remote untainted binary.ttf into a clients text based HTML editor. So follow the guides for adding more than the font-less 14 Standard Fonts embedded in PDF Viewers / Reader-editors.
Beware as shown above some fonts may not work well without some fettling so for example this Ballet font thinks its
Ball et