Hi noticed in my error logs the following error.
Error: WinAnsi cannot encode "
"
at Encoding.encodeUnicodeCodePoint (/app/node_modules/@pdf-lib/standard-fonts/lib/Encoding.js:22:23)
at StandardFontEmbedder.encodeTextAsGlyphs (/app/node_modules/pdf-lib/cjs/core/embedders/StandardFontEmbedder.js:82:41)
at StandardFontEmbedder.widthOfTextAtSize (/app/node_modules/pdf-lib/cjs/core/embedders/StandardFontEmbedder.js:35:27)
at PDFFont.widthOfTextAtSize (/app/node_modules/pdf-lib/cjs/api/PDFFont.js:53:30)
at splitIfBigger (/app/api/services/shipping-logs/shipping-logs.class.js:192:35)
at Object.get (/app/api/services/shipping-logs/shipping-logs.class.js:141:5)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async /app/api/services/orders/hooks/orders.send-email.js:194:37
I assume it is because someone tried to add a non-existing font by copy and pasting test into an input field. What could be the solution?
@MarcGodard You can access the characters supported by a given font with PDFFont.getCharacterSet(). You could then filter out any input characters that are not supported by your font before attempting to draw text with it. You could also substitute them, throw errors, or any number of things. Really depends on your use case.
I hope this helps!
@Hopding I have implemented a system that checks the chars and removes ones (replaces with a "?") that are not in it, but I am still getting this error occasionally:
Error: WinAnsi cannot encode "
" (0x000a)
at Encoding.encodeUnicodeCodePoint (/app/node_modules/@pdf-lib/standard-fonts/lib/Encoding.js:23:23)
at StandardFontEmbedder.encodeTextAsGlyphs (/app/node_modules/pdf-lib/cjs/core/embedders/StandardFontEmbedder.js:88:41)
at StandardFontEmbedder.widthOfTextAtSize (/app/node_modules/pdf-lib/cjs/core/embedders/StandardFontEmbedder.js:36:27)
at PDFFont.widthOfTextAtSize (/app/node_modules/pdf-lib/cjs/api/PDFFont.js:53:30)
Any idea why?
@MarcGodard That's the Unicode character for a newline (I know because it just happened to me).
You can try replacing it with a space character, or else split the text and add the paragraphs separately:
string.split('\n').forEach(paragraph => page.drawText(paragraph))
Most helpful comment
@MarcGodard You can access the characters supported by a given font with
PDFFont.getCharacterSet(). You could then filter out any input characters that are not supported by your font before attempting to draw text with it. You could also substitute them, throw errors, or any number of things. Really depends on your use case.I hope this helps!