How: output euro symbol in pdfkit for nodejs

Is it possible to display the euro symbol in pdfkit for nodejs without having to embed an external font?

I use pdfKit to create invoices and would like to prefix the amount of my currency with the Euro symbol (€).

I tried several approaches and did not work:

doc.font('Helvetica-Bold') .fontSize(12) .text('€', 10, 10); // Alt+0128 on keypad doc.font('Helvetica-Bold') .fontSize(12) .text('\u20AC', 10, 10); 
+4
source share
1 answer

Turns out this is a font issue:

unicode works, but you must make sure that the font you use includes the characters you want to use. Unlike your operating system, PDFKit does not automatically replace the font.

Source: Reddit / u / devongovett Comment


I tested two fonts that were included in the pdfkit file. Both Helvetica-Bold and Times-Roman did not work with unicode characters. I noticed in the documentation for fonts that you can add to your own fonts, so I gave Cardo Font (hosted on Google Fonts) as it supports a lot of Unicode characters.

Of course it worked. Here is the script I used for testing (make sure you have the Cardo font):

 var PDFDocument = require('pdfkit'); var doc = new PDFDocument(); doc.registerFont('Cardo', 'Cardo/Cardo-Regular.ttf') doc.font('Cardo') .fontSize(20) .text('Testing [\u20AC]', 10, 10); doc.write('out.pdf'); 

If you are configured to use Helvetica-Bold, download a copy of the font elsewhere (make sure that it supports the unicode characters that you have after it) and register it as I have with the Cardo font.

+10
source

Source: https://habr.com/ru/post/1501467/


All Articles