如何在带有UTF-8的jsPDF中使用"setFont"?

人气:57 发布:2023-01-03 标签: typescript angular jspdf jspdf-autotable

问题描述

我的项目:角度7

我想将自定义字体(UTF-8)添加到生成PDF的服务中。我使用jsPDF。我找到了很多例子,但都不适用于我。在GitHub jsPDF文档中写道(UTF-8/TTF使用一章): https://github.com/MrRio/jsPDF

这意味着我可以做到这一点。所以我生成了这个文件(字体Roboto-Regular.tff)。但我应该将此文件粘贴到哪里以及如何使用它?

推荐答案

这很简单:

创建函数somePDFCreator并检查可用的字体console.log()
     import jsPDF from 'jspdf';

     function somePDFCreator() {
          const doc = new jsPDF();
          console.log(doc.getFontList());
        }
添加您从该网站生成的font Roboto-regular.tff:https://rawgit.com/MrRio/jsPDF/master/fontconverter/fontconverter.html 并将Lifejs代码插入somePDFCreator。检查日志,必须有您的字体。
    import jsPDF from 'jspdf';   

    function somePDFCreator() {
       (function (jsPDFAPI) { var font = 'AAEAAAAQAQAABAAAR......';
       var callAddFont = function () {
         this.addFileToVFS('Roboto-regular.tff', font);
         this.addFont('Roboto-regular.tff', 'Roboto-Regular', 'normal');
       };
       jsPDFAPI.events.push(['addFonts', callAddFont])
       })(jsPDF.API);


       const doc = new jsPDF();
       console.log(doc.getFontList());
    }
使用导入的字体doc.setFont('Roboto-Regular', 'normal');
    import jsPDF from 'jspdf';   

    function somePDFCreator() {
       (function (jsPDFAPI) { var font = 'AAEAAAAQAQAABAAAR......';
       var callAddFont = function () {
         this.addFileToVFS('Roboto-regular.tff', font);
         this.addFont('Roboto-regular.tff', 'Roboto-Regular', 'normal');
       };
       jsPDFAPI.events.push(['addFonts', callAddFont])
       })(jsPDF.API);


       const doc = new jsPDF();
       console.log(doc.getFontList());

       doc.setFont('Roboto-Regular', 'normal');
       doc.setFontSize(8);

       doc.text("Example text Lorem ipsum", 40, 25);
       return doc.save('myPdfFileName.pdf');
    }

16