I am using the jsPDF library with the fromHtml plugin to generate a pdf document from HTML content and it works well.
Now I am also trying to add a custom font with this plugin: https://github.com/sphilee/jsPDF-CustomFonts-support
However, the two plugins don't seem to interact well with each other, when I generate the pdf it switches to a standard font.
When I use the custom font plugin alone, it works.
This is my code:
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.addFileToVFS('CustomFont.tff', 'base64 of .tff file');
pdf.addFont('CustomFont.tff', 'CustomFont', 'normal');
pdf.setFont('CustomFont');
var source = $('#pdf')[0];
var margins = {
top: 50,
bottom: 60,
left: 40,
width: 520
};
pdf.fromHTML(
source,
margins.left,
margins.top, {
'width': margins.width,
'elementHandlers': specialElementHandlers
},
function (dispose) {
pdf.save('Test.pdf');
}, margins);
I also faced that issue today. Turns out jsPDF has built-in support for custom fonts. So you don't need to include
jspdf.customfonts.jsanymore.You still can use jspdf-customfonts to generate
default_vfs.jsfile:Then in your code you could use that font using the following code:
Instruction on how to generate
default_vfs.js:npm installinside of thenode_modules\jspdf-customfontsdirectorynode makeFonts.jsto createdefault_vfs.js.