DEV Community

Cover image for How to add Arabic Text on PDF with reportlab
Abderrahmane Mustapha
Abderrahmane Mustapha

Posted on • Updated on

How to add Arabic Text on PDF with reportlab

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

import arabic_reshaper
from bidi.algorithm import get_display
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode

Styling the text

first let's initialize our stylesheet

#init the style sheet
styles = getSampleStyleSheet()
Enter fullscreen mode Exit fullscreen mode

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"
)
Enter fullscreen mode Exit fullscreen mode

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 = []
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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

Top comments (5)

Collapse
 
mrahmedelsayed profile image
Mr Ahmed Elsayed

Thank you bro, you saved my day

Collapse
 
abderahmane profile image
Abderrahmane Mustapha

your welcome , any feedbacks !

Collapse
 
arwagh profile image
Arwa

Thank you! but I face a problem when the text contains "الله" word! any idea how to solve this problem?

Collapse
 
abderahmane profile image
Abderrahmane Mustapha

hi @Arwa can you text me on my twitter twitter.com/Abderrahmaneend
or send me an email here abdmusttoumi@gmail.com

Some comments may only be visible to logged-in visitors. Sign in to view all comments.