如何:在pdfkit中为Node.js输出欧元符号 [英] How to: output Euro symbol in pdfkit for nodejs

查看:126
本文介绍了如何:在pdfkit中为Node.js输出欧元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在pdfkit中为nodejs显示欧元符号而无需嵌入外部字体?

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

我正在使用pdfKit生成发票,并想在我的货币金额前加上欧元符号(€).

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);

推荐答案

原来是字体问题:

unicode可以使用,但是您必须确保所使用的字体 包括您要使用的字符.与您的操作系统不同, PDFKit不执行任何自动字体替换.

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.

来源: Reddit Thread 评论由/u/devongovett

我测试了pdfkit随附的两种字体. "Helvetica-Bold"和"Times-Roman"均不适用于unicode符号.我在字体文档中注意到,您可以添加自己的字体,所以我给了Cardo字体(托管在Google字体上),因为它支持许多Unicode字符.

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.

果然,它奏效了.这是我用于测试的脚本(请确保您拥有Cardo字体可用):

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');

如果您打算使用Helvetica-Bold,请在其他位置下载该字体的副本(确保它支持您要使用的Unicode字符),然后将其注册为Cardo字体.

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.

这篇关于如何:在pdfkit中为Node.js输出欧元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆