4

在使用 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。系统是 Ubuntu 11.04 64 位。.oy 文件也是 UTF-8 编码的。在 Document Viewer 2.32.0、Okular 0.12.2 和 Adob​​e Reader 9 中查看 PDF 时,黑框可见。

非常感谢任何帮助,谢谢。

4

1 回答 1

1

您应该像官方示例“附加字体”中那样指定字体名称。使用additional_fontsdefault_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 附带的)

于 2011-08-31T06:22:43.813 回答