How to: nodejs pdfkit output japanese or chinese

て烟熏妆下的殇ゞ 提交于 2019-12-05 03:47:27

问题


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

then the meta-info of the file and the only line of the content shows: "kf Y’˛" which is should be : "武 大郎"

so, is there any way to set the encoding in pdfkit? or some work around?


回答1:


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)

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('武大郎')


来源:https://stackoverflow.com/questions/17206442/how-to-nodejs-pdfkit-output-japanese-or-chinese

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