How to add Arabic Text on PDF with reportlab 
Tags: python, django, arabic, pdf
Published: 2020-05-17 18:24:30

**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](https://dev-to-uploads.s3.amazonaws.com/i/i1jtiovg3m8i6a4jz5wu.PNG)
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 
```shell
pip install reportlab
```

## Let's import the reportlab packages that we are going to use 
   ```python 
   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
```shell
pip install arabic_reshaper
pip install python-bidi
```
now we can import this two packages by adding this two lines of code
```python
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 !](https://www.arfonts.net/f:29lt-bukra-regular)
 now let's add the font and give it the name of **Arabic**
```python
pdfmetrics.registerFont(TTFont('Arabic', 'path to the font directory/29ltbukraregular.ttf'))
```
## Styling the text
 first let's initialize our stylesheet
```python 
#init the style sheet
styles = getSampleStyleSheet()
```

now let's start styling our text
```python
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**

```python
storys = []
```
now let's add some paragraphs to our pdf document we will use ** Spacer ** to set the space between paragraphs

```python

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

```python
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](https://github.com/abderrahmaneMustapha/add-arabic-reportlab/blob/master/screenshots/full.PNG?raw=true)

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

if you use just the  arabic-reshaper 

```python
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](https://github.com/abderrahmaneMustapha/add-arabic-reportlab/blob/master/screenshots/arabic%20reshaper%20alone.PNG?raw=true)

and if we only use bidi 

```python
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](https://github.com/abderrahmaneMustapha/add-arabic-reportlab/blob/master/screenshots/bidi%20alone.PNG?raw=true)


## 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](https://docs.djangoproject.com/en/3.0/howto/outputting-pdf/)

if you want know more about reportlab check the [repotlab user guide](https://www.reportlab.com/docs/reportlab-userguide.pdf)