[英]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.