Here is a simple guide to create your own PDF:
- To start, you need to create a function called
somePDFCreator
. Inside this function, check the available fonts using console.log()
:
import jsPDF from 'jspdf';
function somePDFCreator() {
const doc = new jsPDF();
console.log(doc.getFontList());
}
- The next step is to add a custom font file named
font Roboto-regular.tff
, which can be generated from this website: . Then, insert an IIFE js code block into the somePDFCreator
function. Make sure to verify that your font is added successfully by checking the log.
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());
}
- Finally, utilize your imported font by setting it with
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');
}