ReportLab:阿拉伯字符显示为黑色方块. [英] ReportLab: Arabic characters are displayed as black squares.

查看:72
本文介绍了ReportLab:阿拉伯字符显示为黑色方块.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了数周以阿拉伯语创建 pdf 报告,但我失败了.我使用 ReportLab 和两个包来构建阿拉伯字符,即 bidi.algorithm 和 arabic_reshaper.在控制台中,字符组织得很好,但在 pdf 中只有黑色方块.

I try for weeks to create pdf reports in Arabic, but I failed. I use ReportLab with two packages for building the Arabic characters namely bidi.algorithm and arabic_reshaper. In the console the characters are well organized but in the pdf there are only black square.

import reshaper
from bidi.algorithm import get_display
heading = get_display(reshaper.reshape(unicode('العربية', encoding='utf-8')))
print heading

控制台输出:العربية

The output in console : العربية

但是在生成的pdf文件中: ▀ ▀ ▀ ▀ ▀

But in the generated pdf file : ▀ ▀ ▀ ▀ ▀

提前致谢.

推荐答案

我遇到了同样的问题并想出了以下解决方案:

I faced the same problem and came up with the following solution:

import reshaper
from bidi.algorithm import get_display
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase.ttfonts import TTFont

arabic_text = reshaper.reshape(u'العربية')
arabic_text = get_display(arabic_text)
pdfmetrics.registerFont(TTFont('Arabic-bold', '/path-to-your-arabic-font'))

然后你必须设置Font 'Arabic-bold' 以在pdf文件中显示它.

Then you have to setFont 'Arabic-bold' for displaying it in pdf file.

这篇关于ReportLab:阿拉伯字符显示为黑色方块.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆