Indraraj26
Indraraj26

Reputation: 1976

jspdf fallback font for other language

We use custom font let's say sans that does not support chinese language. How do i add fallback option in the jspdf that it will not use sans font for chinese.

    doc.addFileToVFS('custom-sans-normal.ttf', customSansFont);
    doc.addFont('custom-sans-normal.ttf', 'custom-sans', 'normal');
    doc.setFont("custom-sans", 'normal');

Upvotes: 0

Views: 96

Answers (1)

K J
K J

Reputation: 11847

jsPDF uses the default Adobe PDF font system 14 basic fonts.

Which means any European or CJK fonts must be embedded correctly. Since the only "fall back" is to Latin characters such as Greek Symbols or Partial Roman Times / Helvetica / Courier.

ALL other fonts must be provided as compatible for embedding as described for UTF-8 fonts.

This has been updated and covered in "stackoverflow" many times and well documented at this link.
https://github.com/parallax/jsPDF?tab=readme-ov-file#use-of-unicode-characters--utf-8

Basically,

fontconverter will create a js-file with the content of the provided ttf-file as base64 encoded string and additional code for jsPDF. You just have to add this generated js-File to your project. You are then ready to go to use setFont-method in your code and write your UTF-8 encoded text.

Alternatively you can just load the content of the *.ttf file as a binary string using fetch or XMLHttpRequest and add the font to the PDF file:

Thus you can add multiple fonts e.g. 4 for each desired language (Normal/Regular, Bold , Italic Bold Italic) by using different names.

Upvotes: 0

Related Questions