简体   繁体   English

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

[英]Unicode characters are boxes in Geraldo/ReportLab generated PDF

I'm running into some Unicode related issues when generating PDF reports using Geraldo and ReportLab. 在使用Geraldo和ReportLab生成PDF报告时,我遇到了一些与Unicode相关的问题。

When Unicode strings containing Asian characters are passed into the report, they appear in the output PDF as black boxes. 当包含亚洲字符的Unicode字符串传递到报表中时,它们在输出PDF中显示为黑框。 This example (http://dl.dropbox.com/u/2627296/report.pdf) was generated using the following code: 使用以下代码生成此示例(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')

I'm using Python 2.7.1, Geraldo 0.4.14 and ReportLab 2.5. 我使用的是Python 2.7.1,Geraldo 0.4.14和ReportLab 2.5。 System is Ubuntu 11.04 64-bit. 系统是64位的Ubuntu 11.04。 The .oy file is also UTF-8 encoded. .oy文件也是UTF-8编码的。 The black boxes are visible when the PDF is viewed in Document Viewer 2.32.0, Okular 0.12.2 and Adobe Reader 9. 在Document Viewer 2.32.0,Okular 0.12.2和Adobe Reader 9中查看PDF时,黑框是可见的。

Any help is greatly appreciated, thanks. 非常感谢任何帮助,谢谢。

You should specify the font name as in the official example " Additional Fonts ". 您应该在官方示例“ 附加字体 ”中指定字体名称。 Use additional_fonts and default_style : 使用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() also has a named parameter style : ObjectValue()也有一个命名参数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) 这个字体是开源的,可以在这里下载: http//sourceforge.net/projects/wqy/files/ (我认为它是随Ubuntu 11.04一起提供的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM