I am working on a project to print words in arabic on a PDF file using FPDF2 in python.
but i have faced a lot of problems in showing the language in its original direction.
FPDF2 shows everything language in english direction.
this is how you read english :
and this is how you should read arabic:
but when i put the word above in arabic i face a problem >>>>
The problem is FPDF Shows arabic like this :
1- Tax Before Discount
2- The Total Without
But I need it to be like this:
1- The Total Without
2- Tax Before Discount
i wrote it in english to the problem be understandable.
And This is the problem in arabic language :
But Why This Problem Comes ?
because FPDF2 shows arabic like it shows english, it shows it in the same direction.
This is my code:
from fpdf import FPDF
import arabic_reshaper
def arabic(text):
if text.isascii():
return text
else:
reshaped_text = arabic_reshaper.reshape(text)
return reshaped_text[::-1]
pdf = FPDF()
pdf.add_page()
pdf.add_font(family='DejaVu', style='', fname="DejaVuSans.ttf")
pdf.add_font(family='DejaVu', style='B', fname="DejaVuSans.ttf")
pdf.set_font(family='DejaVu', style='', size=55)
pdf.set_margin(5.0)
with pdf.table(cell_fill_color=(200,200,200), cell_fill_mode="ALL", text_align="CENTER") as table:
names_row = table.row()
names_row.cell(text=arabic("الإجمالي بدون الضريبة قبل الخصم"))
pdf.set_fill_color(r=255,g=255,b=255)
row = table.row()
row.cell(text=str(5000))
pdf.output('final.pdf')
The result of the code :
The problem will be shown when you have to separate the text to multi lines not in one line.
i have tried a lot of solutions but no one of them made me close.
Thanks.
The Reason of the why this happens that FPDF when it wants to break the text ot lines it goes from left to right to break it like this :
but in arabic you should break the text to lines from right to left , but FPDF does not do that it deal with it like it is an english text and it breaks it from left to right like this :
this is a problem in the FPDF library.
how i solved the problem ?
You should edit the library it self , not how you enter the text into the library.
then how to edit the library ?
when you are puting a text in a table inside a cell you are using multi_cell to make that text breakable automaticly by the library.
then you have to go to the function that it deals with the multi_cell, and edit it to when this function takes arabic it has to reverse the the lines and add them into the cell from bottom to up.
the lines before adding them into the cell will be like this :
and this is the lines in arabic :
now we will fuces on the list of lins to solve our problem.
the list of lins
like we have said that you should reverse the list of lines before adding them into the final cell , and when you reverse them they will be like this :
and this is the solusion in theory.
but how can i edit the function multi_cell in FPDF to when the enterd text is english the list will be like what it is, But when the enterd text is arabic the list will be reversed, how can we do that ?
to explain everything that will be dificult but i will show you the code that i added in the function multi_cll to do what i want.
this is the code that i have added in the function multi_cell :
and this is where i puted it inside the function to be working well :
This is the edited function:
This is everything. Thanks