PDF 生成字体处置

YoungKing

YoungKing <[email protected]>
sender-time     Sent at 10:14 (GMT+08:00). Current time there: 10:18 AM. ✆
reply-to        [email protected]
to      [email protected]
date    Thu, Dec 24, 2009 at 10:14
subject Re: [CPyUG:113332] Re: python+django生成pdf文件,汉字处理有什么好办法?

使用自定义字体:
import os
from reportlab.pdfbase import pdfmetrics,ttfonts
pdfmetrics.registerFont(ttfonts.TTFont('song',os.path.join(folderFonts, 'wqy-zenhei.ttc'))) //folderFonts为自定义字体文件夹

使用内嵌字体:
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))

最后加上这两行:
import reportlab.lib.fonts
reportlab.lib.fonts.ps2tt = lambda psfn: ('STSong-Light', 0, 0)
reportlab.lib.fonts.tt2ps = lambda fn,b,i: 'STSong-Light'

如果使得中文换行正确的话,还需再加上这两行:
## for CJK Wrap
import reportlab.lib.styles
reportlab.lib.styles.ParagraphStyle.defaults['wordWrap'] = "CJK"


反馈

创建 by -- ZoomQuiet [2009-12-24 02:18:50]

MiscItems/2009-12-24 (last edited 2009-12-25 07:13:49 by localhost)