Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

node.js - How to: 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 am using pdfKit to generate invoices and would like to prefix my currency amounts with the Euro Symbol (€).

I've tried a number of approaches and none worked:

doc.font('Helvetica-Bold')
    .fontSize(12)
    .text('€', 10, 10); // Alt+0128 on keypad

doc.font('Helvetica-Bold')
    .fontSize(12)
    .text('u20AC', 10, 10);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...