无法在Python中使用FPDF打印特定字符

发布于 2025-01-16 00:50:39 字数 714 浏览 1 评论 0原文

我正在尝试用阿拉伯语打印这个单词

الله

,我尝试了很多字体,但 FPDF 仍然将其打印为盒子(豆腐盒,我猜它就是这么叫的)

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")

如果我不使用阿拉伯语重塑器,结果是 输入图像描述这里这不是输入的正确单词。

结果是这个框在此处输入图像描述

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 herewhich is not the correct word as entered.

Result is this boxenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2025-01-23 00:50:39

我认为解决方案是这样的:

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')

我无法让它与您提供的字体一起使用,但这里使用的谷歌字体可以免费下载。看来 '???' 的某些部分导致了该问题,因为像 '???????' 这样的内容在 Deja Vu Sans Condensed 中确实有效。

filterwarnings("ignore") 之所以存在,是因为 pdf.output 生成的警告似乎不会影响结果,但您可能希望查看它们,而不是仅仅忽略他们:

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

但是,脚本现在似乎可以执行您想要的操作,还显示固定方向之前的初始外观和重塑的外观。

您的问题不同,但我在这里找到了解决方案: 使用 Python pyFPDF 在 PDF 中编写英语和阿拉伯语混合文本时出现问题。 Deja Vu 也出现了该警告,因此它不是由特定字体引起的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文