请看这个 fiddle 示例。我对处理 pdf 字体的知识较少。我正在使用 pdfkit 生成 pdf。我试图加载日语字体,但没有成功。我遵循了 stackoverflow 上的大部分文章,但无法将日语文本写入 pdf。
var doc = new PDFDocument();
var stream = doc.pipe(blobStream());
// draw some text
doc.fontSize(25)
.text("hello world", 100, 80);
var oReq = new XMLHttpRequest();
oReq.open("GET", "//fonts.gstatic.com/ea/mplus1p/v1/Mplus1p-Thin.ttf", true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
doc.registerFont('OpenSans', arrayBuffer)
doc.fontSize(25);
doc.font('OpenSans').text('こんにちは世界')
}
};
oReq.send(null);
// end and display the document in the iframe to the right
doc.end();
stream.on('finish', function () {
document.getElementById('myiframe').src = stream.toBlobURL('application/pdf');
console.log(stream.toBlobURL('application/pdf'))
document.getElementById('spnmessage').innerText = "can't load the pdf in iframe in the jsfiddle. You can open the pdf through console"
});
这里是 fiddle
registerFont
的调用似乎不遵循pdfkit文档所说应该传递的参数。最后,你确定你加载的字体通过了正确的字体验证(使用TTX或其他工具),因为你并没有将其用作Web字体,而是尝试将其作为完整的OpenType字体来排版。 - Mike 'Pomax' Kamermans