如何在带有UTF-8的jsPDF中使用";setFont";? [英] How to use "setFont" in jsPDF with UTF-8?
本文介绍了如何在带有UTF-8的jsPDF中使用";setFont";?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的项目:角度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');
}
这篇关于如何在带有UTF-8的jsPDF中使用";setFont";?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文