无法在Python中使用FPDF打印特定字符
我正在尝试用阿拉伯语打印这个单词
الله
,我尝试了很多字体,但 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 which is not the correct word as entered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为解决方案是这样的:
我无法让它与您提供的字体一起使用,但这里使用的谷歌字体可以免费下载。看来
'???'
的某些部分导致了该问题,因为像'???????'
这样的内容在 Deja Vu Sans Condensed 中确实有效。filterwarnings("ignore")
之所以存在,是因为pdf.output
生成的警告似乎不会影响结果,但您可能希望查看它们,而不是仅仅忽略他们:但是,脚本现在似乎可以执行您想要的操作,还显示固定方向之前的初始外观和重塑的外观。
您的问题不同,但我在这里找到了解决方案: 使用 Python pyFPDF 在 PDF 中编写英语和阿拉伯语混合文本时出现问题。 Deja Vu 也出现了该警告,因此它不是由特定字体引起的。
I think the solution is something like this:
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 thepdf.output
generates warnings which seem not to affect the result, but you may wish to look into them instead of just ignoring them: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.