Also for ES6 module users the steps are very similar.
if you are here looking for a solution on Ionic 4 angular typescript like I was, below is the solution that worked for me!
First you need to install the package via npm as below.
npm install jspdf@latest --save
After that as mentioned in my previous reply there is a font converter made by jsPDF Contributers which you can find on here. Use this to upload your font file and include it in your project somewhere easy to reference inside your TS file so it could be in the same folder as the ts file for example.
-fontname-style.js // <<--- Here (This is the file you downloaded from converter)
-custom.page.module.ts
-custom.page.html
-custom.page.scss
-custom.page.spec.ts
-custom.page.ts
Now in your custom.page.ts import the following;
import * as jsPDF from 'jspdf';
import './fontname-style.js';
//OPTIONAL (Only import if you want to use autotable)
import 'jspdf-autotable';
import { autoTable as AutoTable } from 'jspdf-autotable';
Once you do that just check the file you downloaded from the converter and you should see at the very bottom, your font name on the second parameter of below code.
this.addFont('fontFile.ttf', 'fontName' , '.......');
Now you can create a function like this inside the custom.page.ts;
//custom.component.ts or custom.page.ts etc.
exportPDF(){
var doc = new jsPDF("p","pt","a4"); // You can set your own paramaters
doc.setFont('fontName');
doc.text('This is a test', 20,20); // You can set your own margins
return doc.save('filename.pdf');
}
Now you should see a pdf with 20px left 20px top margin ssaying "This is a test" with your font style :)
OPTIONAL PLUGIN - AutoTable for jsPDF
If you want to use autotable plugin for jsPDF, install the following package from npm.
npm install jspdf-autotable@latest --save
Once installed add this two imports to your custom.page.ts.
import 'jspdf-autotable';
import { autoTable as AutoTable } from 'jspdf-autotable';
You can create a data of columns and headers if you like
and inside the exportPDF() function we created before add those lines;
//custom.component.ts or custom.page.ts etc.
exportPDF(){
var doc = new jsPDF("p","pt","a4");
doc.setFont('fontName');
doc.text('This is a test', 20,20); // <-- Remove this line
((doc as any).autoTable as AutoTable)({
head: [['Name', 'Email', 'Country']], // <-- Table headers
body: [ // <-- Table body
['David', 'david@example.com', 'Sweden'],
['Castille', 'castille@example.com', 'Norway'],
// ...
],
styles: { font: "fontName" } /* <-- This is for the font to work in your
table */
});
return doc.save('filename.pdf');
}
Now you should get a pdf with the table inside it using your font :)
I hope this helps to someone.