![](/img/trans.png)
[英]Reportlab - Unicode characters appear as boxes in unicode-supported font
[英]Unicode characters are boxes in Geraldo/ReportLab generated PDF
在使用Geraldo和ReportLab生成PDF报告时,我遇到了一些与Unicode相关的问题。
当包含亚洲字符的Unicode字符串传递到报表中时,它们在输出PDF中显示为黑框。 使用以下代码生成此示例(http://dl.dropbox.com/u/2627296/report.pdf):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from geraldo import Report, ReportBand, ObjectValue
from geraldo.generators import PDFGenerator
class UnicodeReport(Report):
title = 'Report'
class band_detail(ReportBand):
elements = [ObjectValue(attribute_name='name')]
if __name__ == '__main__':
objects = [{'name': u'한국어/조선말'}, {'name': u'汉语/漢語'}, {'name': u'オナカップ'}]
rpt = UnicodeReport(queryset=objects)
rpt.generate_by(PDFGenerator, filename='/tmp/report.pdf')
我使用的是Python 2.7.1,Geraldo 0.4.14和ReportLab 2.5。 系统是64位的Ubuntu 11.04。 .oy文件也是UTF-8编码的。 在Document Viewer 2.32.0,Okular 0.12.2和Adobe Reader 9中查看PDF时,黑框是可见的。
非常感谢任何帮助,谢谢。
您应该在官方示例“ 附加字体 ”中指定字体名称。 使用additional_fonts
和default_style
:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from geraldo import Report, ReportBand, ObjectValue
from geraldo.generators import PDFGenerator
class UnicodeReport(Report):
title = 'Report'
additional_fonts = {
'wqy': '/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc'
}
default_style = {'fontName': 'wqy'}
class band_detail(ReportBand):
elements = [ObjectValue(attribute_name='name')]
if __name__ == '__main__':
objects = [{'name': u'한국어/조선말'}, {'name': u'汉语/漢語'}, {'name': u'オナカップ'}]
rpt = UnicodeReport(queryset=objects)
rpt.generate_by(PDFGenerator, filename='/tmp/report.pdf')
ObjectValue()
也有一个命名参数style
:
elements = [ObjectValue(attribute_name='name', style={'fontName': 'wqy'})]
这个字体是开源的,可以在这里下载: http : //sourceforge.net/projects/wqy/files/ (我认为它是随Ubuntu 11.04一起提供的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.