jsPDF: Custom font not working

I am using the latest version of jsPDF, and trying to use a custom font from Google but it does not work. I import the font here (the one I’m trying to use is Inconsolata). <link href="https://fonts.googleapis.com/css?family=Source+Serif+Pro|Inconsolata" rel="stylesheet">

I try to add it with this code and it does show up in the output from getFontList, but the PDF doesn’t use that font. doc.addFont('Inconsolata','Inconsolata','regular'); console.log(doc.getFontList()); doc.setFont('Inconsolata');

In addition, I get the following error message on the console (which does not appear if I go back to an old version of jsPDF): jspdf.min.js:102 Uncaught TypeError: Cannot set property 'adler32cs' of undefined at t (jspdf.min.js:102) at jspdf.min.js:102 at jspdf.min.js:1 at jspdf.min.js:1 t @ jspdf.min.js:102 (anonymous) @ jspdf.min.js:102 (anonymous) @ jspdf.min.js:1 (anonymous) @ jspdf.min.js:1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (2 by maintainers)

Most upvoted comments

Currently none. But with 1.4.0 we will have ttf support. But they have to be base64 encoded etc…

I can’t get this to work with the font I want to use, which is Inconsolata from Google Fonts. I encoded it using this site: https://www.giftofspeed.com/base64-encoder/.

I modified the demoUsingTTFFont function to use that font, shown below.

Attached is the PDF that I got. I tried a couple of other fonts and got an empty PDF. So what is the problem here? test.pdf

` function demoUsingTTFFont() { var Inconsolata=“Removed by Aras” var doc = new jsPDF();

doc.addFileToVFS("Inconsolata.ttf", Inconsolata);

doc.addFont('Inconsolata.ttf', 'Inconsolata', 'normal');

doc.setFont('Inconsolata'); // set font

doc.setFontSize(10);
doc.text("This is a test of custom font Inconsolata", 10, 10);

doc.save('test.pdf');

} `

doc.setFont(‘luximr’, ‘normal’) is probably better. I will probably set setFontStyle as deprecated.