3
votes

I'm trying to use a custom font with jspdf but i can't get to show the font as bold, I followed the steps specified here.

here is the code:

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)

Nothing in the pdf appears as bold.

I'm using it in electron, I don't know if that changes anything.

3

3 Answers

1
votes

You can edit your JS file like this,

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');

Output here, Result output

0
votes

You could use: doc.setFont(undefined, 'bold').text("Your paragraph").setFont(undefined, 'normal') to set only one line in bold style

-1
votes

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

doc.setFontType("bold"); doc.text(20, 20, "This is a bold paragraph");

doc.setFontType("normal") doc.text(40, 40, "This is a normal font paragraph");