How to: output Euro symbol in pdfkit for nodejs

≡放荡痞女 提交于 2019-11-29 04:49:45

Turns out it is a font issue:

unicode works, but you have to make sure that the font you are using includes the characters you want to use. Unlike your operating system, PDFKit does not do any automatic font substitution.

Source: Reddit Thread comment by /u/devongovett


I tested two fonts that were included with pdfkit. Both 'Helvetica-Bold' and 'Times-Roman' didn't work with the unicode symbols. I noticed in the documentation for fonts that you can add in your own fonts, so I gave the Cardo Font (hosted on Google Fonts) a go as it supports many unicode characters.

Sure enough, it worked. Here is the script I used for testing (Make sure you have the Cardo font available):

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're set on using Helvetica-Bold, download a copy of the font elsewhere (make sure it supports the unicode characters you're after) and register it as I have with the Cardo font.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!