2011-08-31 125 views
3

使用Geraldo和ReportLab生成PDF报告时遇到了一些Unicode相关的问题。Unicode字符是在Geraldo/ReportLab中生成的PDF格式的PDF

如果将包含亚洲字符的Unicode字符串传递到报告中,它们将作为黑盒出现在输出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,杰拉尔0.4.14和ReportLab的:使用以下代码生成该示例(http://dl.dropbox.com/u/2627296/report.pdf) 2.5。系统是Ubuntu 11.04 64位。 .oy文件也是UTF-8编码的。在Document Viewer 2.32.0,Okular 0.12.2和Adobe Reader 9中查看PDF时,可以看到黑匣子。

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

+0

您是否在使用[嵌入字体](http://forums.adobe.com/thread/370548)?您正在测试的机器上是否安装了[亚洲字体包](http://help.adobe.com/zh_CN/Acrobat/9.0/Standard/WSDCD9813D-3347-4781-810C-5DE967647580.w.html)? –

回答

1

您应该指定正式例子“Additional Fonts”中的字体名称。使用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)

+0

谢谢,现在亚洲人物出现了!在我的设置中,必须运行'sudo apt-get install ttf-wqy-zenhei',因为wqy-zenhei不是标准的CD版本(它在DVD版本上)。这会将字体安装到'/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'。 – NoizWaves