我正在使用带有fromHtml插件的jsPDF库来从HTML内容生成pdf文档,它运行良好。
现在我也尝试使用此插件添加自定义字体:https://github.com/sphilee/jsPDF-CustomFonts-support
但是,这两个插件似乎没有很好地相互作用,当我生成pdf时,它切换到标准字体。
当我单独使用自定义字体插件时,它可以工作。
这是我的代码:
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.addFileToVFS('CustomFont.tff', 'base64 of .tff file');
pdf.addFont('CustomFont.tff', 'CustomFont', 'normal');
pdf.setFont('CustomFont');
var source = $('#pdf')[0];
var margins = {
top: 50,
bottom: 60,
left: 40,
width: 520
};
pdf.fromHTML(
source,
margins.left,
margins.top, {
'width': margins.width,
'elementHandlers': specialElementHandlers
},
function (dispose) {
pdf.save('Test.pdf');
}, margins);
我今天也遇到了这个问题。原来jsPDF内置了对自定义字体的支持。因此,您不再需要包含jspdf.customfonts.js
。
您仍然可以使用jspdf-customfonts来生成default_vfs.js
文件:
(function (jsPDFAPI) {
"use strict";
jsPDFAPI.addFileToVFS('somefont.ttf','AAEAAA...');
})(jsPDF.API);
然后在您的代码中,您可以使用以下代码使用该字体:
pdf.addFont('somefont.ttf', 'somefont', 'normal');
pdf.setFont('somefont');
有关如何生成default_vfs.js
的说明:
npm install
目录中运行node_modules\jspdf-customfonts
node makeFonts.js
创建default_vfs.js
。