2011-08-31 117 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