这是我第一次在这里发布问题。基本上我想要使用 PdfKit(客户端)从浏览器中导出带有日本字符的 pdf 文件。所以我需要通过Buffer将字体文件从服务器发送到客户端。这是我的代码......这都是使用node.js和express:
服务器端(Nodejs):
function download_font(req, res) {
var fontpath = path.join(__dirname, 'kochi-gothic-subst.ttf');
fs.readFile(fontpath, function(err, content) {
if (err) { // If we couldn't read the file for some reason
res.writeHead(404, { // Send a 404 Not Found status
"Content-Type": "text/plain; charset=UTF-8"});
res.write(err.message); // Simple error message body
res.end(); // Done
}
else { // Otherwise, if the file was read successfully.
res.writeHead(200, // Set the status code and MIME type
{"Content-Type": 'application/octet-stream'});
res.write(content); // Send file contents as response body
res.end(); // And we're done
}
});
使用javascript从客户端请求:
$.get("/log/font").done(function( data ) {
var blob = new Blob([data], { type: 'application/octet-stream' });
var doc = new PDFDocument();
var stream = doc.pipe(blobStream());
doc.font(blob).fontSize(25).text('武大郎',100, 100);
doc.end();
stream.on('finish', function() {
blob = stream.toBlob('application/pdf');
var url = window.URL || window.webkitURL;
var link = document.createElement('a');
link.href = url.createObjectURL(blob);
link.download = 'test.pdf';
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, false);
link.dispatchEvent(event);
});
});
但是在运行浏览器时显示错误消息:未捕获的错误:不支持的字体格式或标准 PDF 字体。
所以我从服务器和客户端检查大小数据。这是我检查文件大小的代码:
*$.get("/log/font").done(function( data ) {
//alert( "Data Loaded: " + data );
var blob = new Blob([data], { type: 'application/octet-stream' });
var url = window.URL || window.webkitURL;
var link = document.createElement('a');
link.href = url.createObjectURL(blob);
link.download = 'kochi-gothic-subst.ttf';
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, false);
link.dispatchEvent(event);
});
我看到两个文件不相等。任何人都可以帮助我从浏览器端导出带有日本字符的 pdf 文件吗?