2

I am trying to print this word in Arabic

الله

I tried many fonts but still, the FPDF is printing it as box (tofu box, is what it is called I guess)

from fpdf import FPDF
import arabic_reshaper

pdf = FPDF('P', 'mm', "A4")
pdf.add_page()
pdf.add_font('dejavu', "", "DejaVuSansCondensed.ttf", uni=True)
pdf.set_font('dejavu', '', 10)
pdf.multi_cell(69, 5, arabic_reshaper.reshape("الله"), "", ln=1)
pdf.output("mypdf.pdf")

If I dont use arabic reshaper the result is enter image description here which is not the correct word as entered.

Result is this boxenter image description here

derikS4M1
  • 89
  • 9
  • Check this: https://stackoverflow.com/questions/10028375/how-to-add-new-fonts-to-pyfpdf#:~:text=Go%20to%20your%20installation%20of,it%20in%20the%20fonts%20folder. – gajendragarg Mar 20 '22 at 10:52
  • 1
    I was able to run your code without issues grabbing a copy of that font somewhere online, except that I had to pass `uni=True` to `add_font()` to ensure it loaded with the correct encoding. I could not reproduce your issue - with that option set, the text shows in the PDF. – Grismar Mar 20 '22 at 11:24
  • You used the same font ? DejaVuSansCondensed.ttf ?? – derikS4M1 Mar 20 '22 at 12:39
  • Hi, did you get the result as ```ه ل ل ا``` ?? if yes then thats not correct. I have updated the code with additional info – derikS4M1 Mar 20 '22 at 12:43
  • @Grismar any idea over this issue? – derikS4M1 Mar 20 '22 at 18:28
  • Ah, yes, it did look like that - sorry I don't speak or read the language, so I was unable to tell if that was undesirable or just a stylistic difference. With the reshaper, I do also get the tofu block. Have you checked if the font you picked can actually correctly render the text in something like a word processor? (i.e. MS Word, etc.) – Grismar Mar 20 '22 at 22:37

1 Answers1

4

I think the solution is something like this:

from fpdf import FPDF
from arabic_reshaper import reshape
from bidi.algorithm import get_display
from warnings import filterwarnings

pdf = FPDF()

pdf.add_page()
pdf.add_font("NotoSansArabic", style="", fname="NotoSansArabic-Regular.ttf", uni=True)
pdf.set_font('NotoSansArabic', '', 10)
original_text = 'الله'

reshaped_text = reshape(original_text)
bidi_text = get_display(reshaped_text)

pdf.write(8, original_text)
pdf.ln(8)

pdf.write(8, reshaped_text)
pdf.ln(8)

pdf.write(8, bidi_text)
pdf.ln(8)

filterwarnings('ignore')
pdf.output('mypdf.pdf')
filterwarnings('default')

I could not get it to work with the font you provided, but the Google font used here can be downloaded for free. It appears some part of 'الله' is causing the issue, because something like 'مرحبا' does work in Deja Vu Sans Condensed.

The filterwarnings("ignore") is there because the pdf.output generates warnings which seem not to affect the result, but you may wish to look into them instead of just ignoring them:

[..]\site-packages\fpdf\ttfonts.py:670: UserWarning: cmap value too big/small: -65241
  warnings.warn("cmap value too big/small: %s" % cm)

However, the script now appears to do what you want, also showing the initial look and the reshaped look before fixing the direction.

Your question is different, but I found the solution here: Problem writing a mix of English and Arabic text in PDF using Python pyFPDF. The warning occurred with Deja Vu as well, so it's not caused by the specific font.

Grismar
  • 27,561
  • 4
  • 31
  • 54