Search code examples
javascriptreactjsjspdf

how to add and set a font in JsPDF?


I've been trying to set the Helvetica font in different ways but nothing seems to work. I understand the addFont and setFont function but I'm having problems using them.

This is what I have:

doc.addFont('Helvetica', 'Helvetica', 'normal')
doc.setFont('Helvetica')

Apparently the postscript name for Helvetica is 'Helvetica' so I don't know what it could be.


Solution

  • you can convert your font

    Example PTSans-normal.js

    import jsPDF from 'jspdf' jsPDF - Importing fonts in React.js / ES6 style

    Using a new font

    import 'assets/fonts/PTSans-normal.js';
    import jsPDF from 'jspdf';
    
    const doc = new jsPDF();
    
    doc.setFont('PTSans'); // set custom font
    doc.setFontSize(12); // you can change size
    doc.text('Hello', x, y) // and you can write your text
    
    console.log('Show all font in jsPDF',doc.getFontList());