I generate PDF invoices with ReportLab. When I add Hindi text, it shows as black boxes, and when I register a Hindi font, the letters appear but the matras and joined letters are in the wrong positions. How do I print Hindi correctly with ReportLab?
I generate PDF invoices with ReportLab. When I add Hindi text, it shows as black boxes, and when I register a Hindi font, the letters appear but the matras and joined letters are in the wrong positions. How do I print Hindi correctly with ReportLab?
There are two separate problems here.
1. Black boxes: font not registered
ReportLab's built-in fonts (Helvetica, Times) only contain Latin characters. You must register a TrueType font that includes Devanagari, such as Noto Sans Devanagari, Mukta or Hind:
Then use "NotoDeva" as the font name in your canvas calls or paragraph styles. Keep the font file in your project and use a full path in production.
2. Wrong matras and conjuncts: text shaping
Devanagari needs "complex text shaping": the font's rules move the i-matra before the consonant, form half-letters and conjuncts, and position marks above and below. Classic ReportLab simply placed characters one after another, so Hindi looked broken.
Recent ReportLab versions (4.x) added text shaping support through HarfBuzz. To use it:
If your version cannot shape Devanagari correctly, other options are:
Whatever method you use, embed the font (TrueType fonts are embedded automatically in ReportLab) so the PDF looks the same on every computer.