Unicode 字符是 Geraldo/ReportLab 生成的 PDF 中的方框

发布于 2024-12-02 14:05:45 字数 888 浏览 1 评论 0原文

使用 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 编码的。在文档查看器 2.32.0、Okular 0.12.2 和 Adob​​e Reader 9 中查看 PDF 时,黑框可见。

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

I'm running into some Unicode related issues when generating PDF reports using Geraldo and ReportLab.

When Unicode strings containing Asian characters are passed into the report, they appear in the output PDF as black boxes. This example (http://dl.dropbox.com/u/2627296/report.pdf) was generated using the following code:

#!/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')

I'm using Python 2.7.1, Geraldo 0.4.14 and ReportLab 2.5. System is Ubuntu 11.04 64-bit. The .oy file is also UTF-8 encoded. The black boxes are visible when the PDF is viewed in Document Viewer 2.32.0, Okular 0.12.2 and Adobe Reader 9.

Any help is greatly appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2024-12-09 14:05:45

您应该指定字体名称,如官方示例“其他字体” 。使用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 一起提供)

You should specify the font name as in the official example "Additional Fonts". Use additional_fonts and 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() also has a named parameter style:

elements = [ObjectValue(attribute_name='name', style={'fontName': 'wqy'})]

This font is open source and can be downloaded here: http://sourceforge.net/projects/wqy/files/ (I think it's shipped with Ubuntu 11.04)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文