如何在jspdf中使用粗体自定义字体

时间:2019-01-04 02:40:17

标签: jspdf

我正在尝试将自定义字体与jspdf一起使用,但是我无法将字体显示为粗体,因此请按照here指定的步骤进行操作。

代码如下:

require ("./js/Roboto-Regular-normal.js");
require ("./js/Roboto-Regular-bold.js");
let doc = new jsPDF();

doc.setFont('Roboto-Regular');
doc.setFontSize(8);
doc.text("hello world", 50 , 50);
doc.setFontType('bold');
doc.text("Hello Bold", 50, 55);

let blob = doc.output();
const fs = require('fs');
fs.writeFile('a5.pdf', blob, 'utf8', function(err){
    //Do something
});

Roboto-Regular-normal.js

(function (jsPDFAPI) {
    var font = base64_font; //I didn't include this string because is too large, let me know if I should include it anyways

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

Roboto-Regular-bold.js:

(function (jsPDFAPI) {
        var font = base64_font; //I didn't include this string because is too large, let me know if I should include it anyways

        var callAddFont = function () {
            this.addFileToVFS("Roboto-Regular-bold.ttf", font);
            this.addFont("Roboto-Regular-bold.ttf", "Roboto-Regular", "bold");
        };
        jsPDFAPI.events.push(['addFonts', callAddFont])
})(jsPDF.API)

pdf中的所有内容都不显示为粗体。

我正在电子中使用它,我不知道它是否会改变任何东西。

3 个答案:

答案 0 :(得分:0)

https://github.com/MrRio/jsPDF/issues/199#issuecomment-73009553

doc.setFontType(“ bold”); doc.text(20,20,“这是一个粗体的段落”);

doc.setFontType(“ normal”) doc.text(40,40,“这是普通字体段落”);

答案 1 :(得分:0)

你可以像这样编辑你的 JS 文件,

doc.text("This is example paragraph", 11,13,).setFontSize(8).setFont(undefined, 'bold');

doc.text("This is example paragraph", 11,13,).setFontSize(8).setFont(undefined, 'normal');

在这里输出, Result output

答案 2 :(得分:0)

你可以使用: doc.setFont(undefined, 'bold').text("Your paragraph").setFont(undefined, 'normal') 仅以粗体样式设置一行