请看小提琴的例子。我对使用 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"
});
这是小提琴