parallax/jsPDF

How to make hebrew works

Closed this issue · 2 comments

Hello,
I'm using the last version 2.5.1 but I can't render a PDF with Hebrew, even if I Set the language, for example:

`const { jsPDF } = window.jspdf;

const doc = new jsPDF();
doc.setFont('Arial');
doc.text(10, 10, 'תן אישור כשירות מחדש ע"י רשות הרישוי ינתן עם החזרת טופס הודעה בדבר תקינות הרכב החתום ע"י מכון הרישוי.');
doc.setLanguage("he")
doc.save('MyTest.pdf');
`

What I see in the PDF:

image

Thanks in advance

Hey @p84torres,

I have been using jsPDF for quite some time and have worked with various languages. The default fonts in jsPDF do not support Hebrew or other right-to-left (RTL) languages out of the box. Additionally, setting the language in jsPDF (doc.setLanguage("he")) does not automatically enable proper rendering for non-Latin scripts like Hebrew.


Here’s What You Can Do to Fix It

  1. Download a Hebrew-Supporting Font
  2. Convert the Font to Base64
  3. Embed the Font in jsPDF using addFileToVFS

Sample Code

import { jsPDF } from "jspdf";
// This is the Base64 font.
import hebrewFont from "./font.js"; 

const doc = new jsPDF();
doc.addFileToVFS("hebrew.ttf", hebrewFont);
doc.addFont("hebrew.ttf", "hebrew", "normal");
doc.setFont("hebrew"); // Use the added Hebrew font

const hebrewText = `"תן אישור כשירות מחדש ע"י רשות הרישוי ינתן עם החזרת טופס הודעה בדבר תקינות הרכב החתום ע"י מכון הרישוי."`;

doc.text(hebrewText, 10, 10, { align: "right" }); // Use align "right" for RTL
doc.save("MyTest.pdf");

References

If you need more references, check out my repository: - Recibill
I’ve implemented it here: - Recibill PDF Utils


If you still face issues, then you can go to my profile and you will find my contact details there.

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.