jsPDF - 来自HTML和自定义字体

问题描述 投票:3回答:1

我正在使用带有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);
javascript html css jspdf fromhtml
1个回答
0
投票

我今天也遇到了这个问题。原来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的说明:

  1. npm install目录中运行node_modules\jspdf-customfonts
  2. 将字体复制到fonts子目录中。
  3. 运行node makeFonts.js创建default_vfs.js
© www.soinside.com 2019 - 2024. All rights reserved.