如何:nodejs pdfkit 输出日语或中文 [英] How to: nodejs pdfkit output japanese or chinese
问题描述
我在做我的 nodejs + expressjs + mongodb 项目,我需要从 mongodb 中获取数据,然后将其写入 pdf 文件,然后通过 expressjs 发送.除了数据是日文字母和编码混乱之外,一切似乎都很好.我正在使用 pdfkit 创建 pdf 文件,如下所示:
I'm doing my nodejs + expressjs + mongodb project, I need fetch data from mongodb and then write it to pdf file, then send out by expressjs. everything seems fine except that the data is Japanese letter, and the encoding messed-up. I'm using pdfkit for creating pdf file, like this:
var doc = new PDFDocument();
doc.info['Title'] = profile.firstName + " " + profile.lastName;
doc.fillColor('black')
.text(profile.firstName + " " + profile.lastName, {
paragraphGap: 10,
indent: 20,
align: 'justify',
columns: 2
});
然后文件的元信息和内容的唯一一行显示:kf Y'˛"应该是:武大郎"
then the meta-info of the file and the only line of the content shows: "kf Y’˛" which is should be : "武 大郎"
那么,有没有办法在pdfkit中设置编码?或者一些解决方法?
so, is there any way to set the encoding in pdfkit? or some work around?
推荐答案
PDFKit 支持以 TrueType (.ttf)、TrueType Collection (.ttc) 和 Datafork TrueType (.dfont) 格式嵌入字体文件.(来源:http://pdfkit.org/docs/text.html#fonts)
PDFKit supports embedding font files in the TrueType (.ttf), TrueType Collection (.ttc), and Datafork TrueType (.dfont) formats. (source: http://pdfkit.org/docs/text.html#fonts)
在此处下载 TrueType (.ttf) 格式的日语字体 http://www.freejapanesefont.com/ipaex-哥特式/
Download a Japanese Font in TrueType (.ttf) format here http://www.freejapanesefont.com/ipaex-gothic/
# Using a TrueType font (.ttf)
doc.font('fonts/ipaexg.ttf').text('武大郎')
这篇关于如何:nodejs pdfkit 输出日语或中文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!