javascript - jsPDF use Chinese fonts to build PDF from HTML

I am using WordPress Eventin plugin which embedded jsPDF. I have successfully added Chinese fonts SimHei.ttf to jsPDF like that:

const jsPDF = window.jspdf.jsPDF;
const doc = new jsPDF("p", "pt", "a4");
doc.addFont('https://server/path/Fonts/SimHei.ttf', 'SimHei', 'normal');
doc.setFont('SimHei');
doc.text("Octonyan loves jsPDF ??�?��?��", 35, 25);

But I need to convert HTML to PDF like that:

let getContent = "<p>Octonyan loves jsPDF ??�?��?��</p>";
doc.html(getContent, {
    callback: function () {
        doc.save(ticketname);
    },
    x: 10,
    y: 10,
});

It does not display Chinese to PDF.

Anyone can help output HTML to PDF. Thank you!

  • Downloaded original jsPDF to test
  • Selected usable Chinese fonts
  • Used fontconverter for following test
  • Tried - doc.addFileToVFS("SimHei.ttf", myFont); It also works

Answer

Solution:

Make sure that the compression parameter in jsPDF is set to false when you are creating the object of it called doc. Like this:

const doc = new jsPDF({
  compress: false,
  orientation: 'p',
  unit: 'px',
  format: 'a4'
});

Source