Properly Rendering Arabic Text in Python and PDF with arabic_reshaper and bidi

Uncategorized > Properly Rendering Arabic Text in Python and PDF with arabic_reshaper and bidi
Facebook
Print
LinkedIn
Telegram
X
WhatsApp
0 0
Read Time:3 Minute, 4 Second

If you’ve ever tried printing Arabic text in Python, you’ve probably noticed that it comes out… wrong. The letters are disconnected, flowing in the wrong direction, or jumbled when combined with English text. That’s because most environments don’t natively support Arabic shaping or right-to-left (RTL) rendering.

This post will walk you through how to fix that using two essential tools: arabic_reshaper and python-bidi. We’ll also show how to generate a clean, professional PDF using the reportlab library — with fully correct Arabic rendering.


🤔 Why Doesn’t Arabic Just Work?

Arabic is a cursive script. Unlike Latin characters, Arabic letters:

  • Change shape depending on their position in a word.
  • Flow from right to left.
  • May include ligatures (e.g., لا) and diacritics (e.g., َّ ِ ُ).

Unfortunately, most text-rendering systems treat characters as isolated units and assume left-to-right flow. That leads to broken or reversed Arabic.


🛠 Tools You’ll Need

Install the required libraries:


“`bash
pip install arabic-reshaper python-bidi reportlab

  • arabic_reshaper: Shapes Arabic letters into their correct contextual forms.
  • python-bidi: Handles bidirectional (BiDi) text display.
  • reportlab: Generates PDFs with custom fonts and text layout.

You’ll also need an Arabic-compatible font such as Amiri.


🧪 Reshaping Arabic Text

Let’s start with a simple string:

import arabic_reshaper
from bidi.algorithm import get_display

text = "بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ"

reshaped = arabic_reshaper.reshape(text)
bidi_text = get_display(reshaped)

print(bidi_text)

Why both steps?

  • arabic_reshaper.reshape() connects the letters properly.
  • get_display() reorders the string so it appears correctly in left-to-right renderers like terminals and plotting libraries.

🔍 Even though get_display() returns a string that flows left-to-right, the characters appear right-to-left — exactly what we want.


🖨 Rendering Arabic in PDF

Here’s how to use reportlab to generate a PDF with Arabic text:

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import arabic_reshaper
from bidi.algorithm import get_display

# Register the Arabic font (make sure Amiri-Regular.ttf is in the same folder)
pdfmetrics.registerFont(TTFont('Amiri', 'Amiri-Regular.ttf'))

# Create the canvas
c = canvas.Canvas("arabic_output.pdf")
c.setFont("Amiri", 20)

# Reshape and reorder Arabic text
text = "بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ"
reshaped = arabic_reshaper.reshape(text)
bidi_text = get_display(reshaped)

# Draw the text, right-aligned at x=500
c.drawRightString(500, 800, bidi_text)

c.save()

⚠️ Avoid Overlapping Lines

If you’re drawing multiple lines of Arabic, adjust the Y-position for each line to prevent overlap:

line_spacing = 40
c.drawRightString(500, 800, line1)
c.drawRightString(500, 760, line2)  # 800 - 40

📌 Key Takeaways

TaskTool
Arabic letter shapingarabic_reshaper
Right-to-left displaypython-bidi
PDF renderingreportlab + drawRightString()
Arabic fontsAmiri, Scheherazade, Lateef

✅ Bonus Tip: get_display Returns LTR-Ordered Text

It may seem counterintuitive, but get_display() returns a left-to-right string that renders right-to-left. This is by design — it helps LTR rendering engines (like PDF or PIL) show Arabic visually correct, without needing RTL layout engines.

So don’t panic if your reshaped string looks like:

ﻢﻜﻴﻠﻋ ﻢﻼﺴﻟا

…it will appear as “السلام عليكم” in the final render.


🧱 Next Steps

Now that you know how to render Arabic cleanly:

  • Add multiple verses with spacing
  • Mix Arabic and English text
  • Build PDFs, images, or charts that support Arabic natively

If you’d like a follow-up tutorial on generating paragraphs, tables, or text wrapping in Arabic, let me know in the comments!

featured image : crownaart/iStock/Getty Images
Facebook
Twitter
LinkedIn
Pinterest
Pocket
WhatsApp

Jangan lewatkan artikel penting! Langganan newsletter dosensibuk.com sekarang.

Leave a Reply

Your email address will not be published. Required fields are marked *