← Back to Notes

Why Persian and Arabic text breaks in PDFs — and how to shape it correctly in Python

Why Persian and Arabic text breaks in PDFs — and how to shape it correctly in Python

If you generate PDFs from Python — invoices, reports, certificates, a CV — Persian and Arabic text will punish you in ways Latin text never does. The letters come out disconnected, each one in its isolated form like a child's letter chart. Or the whole sentence runs backwards. Or the English word inside the sentence teleports to the wrong end. I hit every one of these while building the trilingual CV generator behind this site, and each one has a specific cause.

Arabic script is not a row of letters

Latin text is forgiving: one codepoint, one glyph, drawn left to right in the order you stored it. Arabic script does not work that way. Most letters have up to four forms — isolated, initial, medial, final — chosen by their neighbours, and some pairs fuse into ligatures. The text you store is in logical order (the order it is typed and read), but what must reach the page is a sequence of shaped glyphs in visual order.

That conversion is two separate jobs: shaping (picking the right glyph form for every letter, with HarfBuzz as the reference engine) and bidi reordering (applying the Unicode bidirectional algorithm so RTL paragraphs and embedded Latin runs land in the right places). If your PDF code does neither — and most low-level drawing code does neither — you get the letter chart.

Failure 1: drawing codepoints and hoping

The classic first attempt: take the string, set a font that "supports Arabic", and draw it. The output shows every letter in its isolated form, disconnected. Nothing crashed; the library simply placed the base codepoints side by side, because nobody told it to shape. A font containing Arabic glyphs is necessary and nowhere near sufficient.

Failure 2: shaping twice, or shaping by hand

The popular workaround is to pre-process the text yourself with arabic-reshaper plus python-bidi before handing it to the PDF library. This can make a page look right, and it has two costs people discover later. The PDF now contains Arabic Presentation Forms codepoints instead of your original text, so search, copy-paste and screen readers see junk. And the moment you move to a library that shapes properly on its own, the same pre-processing double-shapes the text and it falls apart again — a pipeline has to know exactly which one component owns shaping.

Failure 3: the font quietly lacks the glyphs

If the output shows boxes or blanks only for some words, suspect coverage, not code. Persian needs letters that Arabic fonts sometimes treat as optional — گ چ پ ژ — plus the Persian ی and ک, which are different codepoints from the Arabic ي and ك. I use Vazirmatn for both Persian and Arabic output: it covers both repertoires and embeds cleanly as a TTF.

Failure 4: digits and Latin words in the wrong place

Mixed text is where bidi bugs breed. An email address or a product name inside a Persian sentence is a separate directional run; if the paragraph direction is wrong, it lands on the wrong side. Digits are their own trap: Persian text normally uses ۰۱۲۳۴۵۶۷۸۹, much Arabic text uses ٠١٢٣٤٥٦٧٨٩, and the same number typed with Latin digits will be reordered differently. Decide per language and normalize digits deliberately instead of letting the renderer guess.

The pipeline that works

For this site's CV downloads I generate all three languages from one code path with fpdf2, and let its HarfBuzz engine own shaping end to end:

from fpdf import FPDF

pdf = FPDF()
pdf.add_font("Vazir", "", "Vazirmatn-Regular.ttf")
pdf.add_font("Vazir", "B", "Vazirmatn-Bold.ttf")
pdf.set_text_shaping(use_shaping_engine=True,
                     direction="rtl", script="arab", language="fa")
pdf.set_font("Vazir", size=11)
pdf.multi_cell(w=0, h=6, text="متن فارسی شما", align="R")

Three details do the heavy lifting. The shaping context is set per language — direction, script and language change between the Persian, Arabic and English documents, and the English one runs with direction="ltr", script="latn". The text goes in logical order, untouched — no reshaper, no manual reversal. And wrapping is left to multi_cell after shaping, because line breaking has to measure the shaped glyphs, not the raw string.

The false alarm: extraction is not rendering

One last trap, and it is the one that wastes the most time. After everything renders correctly, you extract the text with pypdf or PyMuPDF to "check" it — and it looks garbled and reversed. That is the extraction layer reporting logical-order text; it is not what the page shows. Verify the rendered page: open it, or measure glyph positions on the rendered output, the way I did when checking this generator's margins. Do not let an extraction artifact talk you into breaking a correct pipeline.

Checklist

  • Shape with a real engine (HarfBuzz), exactly once — never pre-shape text for a shaping library.
  • Embed a font that covers the full repertoire of your language, including the Persian-only letters.
  • Set direction, script and language per text block or document.
  • Keep your strings in logical order from database to PDF call.
  • Normalize digits per locale on purpose.
  • Judge the result on the rendered page, not on extracted text.

The generator behind this site's downloadable CV is part of my portfolio repository, if you want to read a working example end to end.

Contact
Next Note

Why is my LLM stream empty? A field guide to broken SSE responses

→