请看小提琴的例子。我对使用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