2

I use python reportlab(window10, python3.6) to write text in the page, when the text is English it works, but when I use Arabic text it writes messy code in the image.

Below is my code:

from reportlab.lib.units import mm
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
page = canvas.Canvas("test.pdf", pagesize=A4)
page.drawString(10*mm, 267*mm, "اللغة العربية") 
page.showPage()
page.save()

enter image description here

Jona
  • 1,218
  • 1
  • 10
  • 20
OMRAN
  • 61
  • 1
  • 4
  • Please, reformat the question to remove all those `enter code here` and to have the code nicely formatted. What you expect is also not clear. – Jona Jan 24 '20 at 15:08
  • ok, I edit style. like this edit – OMRAN Jan 24 '20 at 15:25
  • Maybe you can find a solution there: [How to create PDF containing Persian(Farsi) text with reportlab, rtl and bidi in python](https://stackoverflow.com/questions/41345450/how-to-create-pdf-containing-persianfarsi-text-with-reportlab-rtl-and-bidi-in) – Jona Jan 24 '20 at 22:28

2 Answers2

1

To solve your problem you must use 4 steps:

  1. Import rtl
from rtl import reshaper
  1. Implement this method:
@staticmethod
def reshape_text(text):
    text = reshaper.reshape(text)
    text = get_display(text)
    get_display() 
    return text
  1. Add your font in pdf. I use FPDF library and in below I append my code
import fpdf
pdf = fpdf.FPDF(format="A4")  # pdf format
pdf.add_font("fontNormal", style="", fname="./Sahel.ttf", uni=True)
pdf.set_font("fontNormal", "", size=int(10))  # font and textsize
  1. At the end you can pass your text to the pdf generator:
pdf.cell(90, 7, txt=self.reshape_text("مهندس علیرضا صفافرد"), ln=1, align="R")
rassar
  • 5,412
  • 3
  • 25
  • 41
0

You have to use a font that is supporting arabic characters. I took the font Scheherazade here as an example (you have to copy Scheherazade-Regular.ttf in site-packages\reportlab\fonts):

from reportlab.lib.units import mm
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont 

pdfmetrics.registerFont(TTFont('Scheherazade', 'Scheherazade-Regular.ttf'))

page = canvas.Canvas("test.pdf", pagesize=A4)
page.setFont('Scheherazade', 12)
page.drawString(10*mm, 267*mm, "اللغة العربية") 
page.showPage()
page.save()

Ref: Report Lab can't handle hebrew (unicode)

Jona
  • 1,218
  • 1
  • 10
  • 20
  • 1
    Please expand this answer to explain what changes you've made and why. Specifically, Scheherazade-Regular is required because you need a font that includes glyphs for Arabic, and the UTF-8 encoding you've added. Otherwise future searchers need to dig through this code to figure out how it differs and guess at why. Thanks for contributing. – Rob Napier Jan 24 '20 at 16:18
  • 2
    thank you for this answer. but it maybe I reach to my need but not exactly the letters which output not like this "اللغة العربية" it output "ا ل غ ة ا ل ع ر ب ي ة" so what is the answer – OMRAN Jan 24 '20 at 21:12