How to add Arabic Text on PDF with reportlab

How to add Arabic Text on PDF with reportlab

Subscribe to my newsletter and never miss my upcoming articles

ReportLab is a tool for creating complex data-driven PDF documents and custom vector graphics but this tool does not support arabic, so when you want add an arabic text you will see something like image of arabic text added with reportlab so how can we fix this in this article i will show you an example how to add an arabic text with reportlab using reportlab.platypus let's start ...

Install ReportLab

if you have both pip and python installed so its easy just do this

pip install reportlab

Let's import the reportlab packages that we are going to use

   from reportlab.lib.styles import getSampleStyleSheet
   from reportlab.lib.pagesizes import letter
   from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer,PageBreak
   from reportlab.lib.styles import ParagraphStyle
   from reportlab.pdfbase import pdfmetrics
   import reportlab
   from reportlab.pdfbase.ttfonts import TTFont

Install arabic-reshaper and python-bidi

we will need this two packages to reshape our text so just go to your command line and type

pip install arabic_reshaper
pip install python-bidi

now we can import this two packages by adding this two lines of code

import arabic_reshaper
from bidi.algorithm import get_display

Using a custom font

here we need to use a custom font because the default font in reportlab does not support arabic im using this font 29ltbukraregular ! now let's add the font and give it the name of Arabic

pdfmetrics.registerFont(TTFont('Arabic', 'path to the font directory/29ltbukraregular.ttf'))

Styling the text

first let's initialize our stylesheet

#init the style sheet
styles = getSampleStyleSheet()

now let's start styling our text

arabic_text_style = ParagraphStyle(
    'border', # border on
    parent = styles['Normal'] , # Normal is a defaul style  in  getSampleStyleSheet
    borderColor= '#333333',
    borderWidth =  1,
    borderPadding  =  2,
    fontName="Arabic" #previously we named our custom font "Arabic"
)

Add the text

most of the time you will want to add more than a one paragraph to your file so we can use an array which contain our paragraphs let's name it storys

storys = []

now let's add some paragraphs to our pdf document we will use Spacer to set the space between paragraphs


arabic_text =""" عندما يريد العالم أن ‪يتكلّم ‬ ، فهو يتحدّث بلغة
 يونيكود. تسجّل الآن لحضور المؤتمر الدولي العاشر ليونيكود (Unicode Conference)، الذي سيعقد في 10-12 آذار 1997 بمدينة مَايِنْتْس، ألمانيا. و سيجمع المؤتمر بين خبراء
  من كافة قطاعات الصناعة على الشبكة العالمية انترنيت ويونيكود، حيث ستتم، على الصعيدين الدولي والمحلي على حد سواء مناقشة سبل استخدام يونكود في النظم القائمة وفيما يخص التطبيقات الحاسوبية، الخطوط، تصميم النصوص والحوسبة متعددة اللغات."""

# reshape the text 
rehaped_text = arabic_reshaper.reshape(arabic_text)
bidi_text = get_display(rehaped_text)

# add the text to pdf
 ## dont forget to add the style arabic_text_style
storys.append(Paragraph(bidi_text,arabic_text_style))
storys.append(Spacer(1,8)) # set the space here
storys.append(Paragraph(bidi_text,arabic_text_style))

finally we will use SimpleDocTemlate to create the document and save it

doc = SimpleDocTemplate('mydoc.pdf',pagesize = letter)
## add the storys array to the pdf document
doc.build(storys)

and this is our final resul perfect ...! arabic text with reportlab

But why we need to use arabic-reshaper and python-bidi !

if you use just the arabic-reshaper

rehaped_text = arabic_reshaper.reshape(arabic_text)
storys.append(Paragraph(rehaped_text,arabic_text_style))
storys.append(Spacer(1,8))
storys.append(Paragraph(rehaped_text,arabic_text_style))

and this is the result using just the arabic reshaper

and if we only use bidi

bidi_text = get_display(arabic-text)

storys.append(Paragraph(bidi_text,arabic_text_style))
storys.append(Spacer(1,8))
storys.append(Paragraph(bidi_text,arabic_text_style))

this is the result using just the python-bidi

For Further exploration

reportlab can be used with djago to generate pdf files and reports and this is a link if you want learn more click here

if you want know more about reportlab check the repotlab user guide

 
Share this