Differences between revisions 7 and 9 (spanning 2 versions)
Revision 7 as of 2008-08-14 00:55:03
Size: 4904
Editor: ZoomQuiet
Comment:
Revision 9 as of 2009-12-25 07:09:01
Size: 4867
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
[[TableOfContents]] <<TableOfContents>>
Line 6: Line 6:
[[Include(ZPyUGnav)]] <<Include(ZPyUGnav)>>
Line 30: Line 30:
 * attachment:html2pdf.7z
 * 最新版本:'''[http://openbookproject.googlecode.com/svn/tangle/html2pdf html2pdf]''' ^@ openbookproject.googlecode.com^
 * [[attachment:html2pdf.7z]]
 * 最新版本:'''[[http://openbookproject.googlecode.com/svn/tangle/html2pdf|html2pdf]]''' ^@ openbookproject.googlecode.com^
Line 121: Line 121:
  1. attachment:usage-py-reportlab-rst-zhPDF_by_rst.pdf
   * attachment:snap-rst-zhPDF_by_rst.png
  1. attachment:usage-py-reportlab-rst-zhPDF_by_OOo2.pdf
   * attachment:snap-rst-zhPDF_by_OOo2.png
  1. attachment:usage-py-reportlab-rst-zhPDF_by_firefox.pdf
   * attachment:snap-rst-zhPDF_by_firefox.png
  1. [[attachment:usage-py-reportlab-rst-zhPDF_by_rst.pdf]]
   * {{attachment:snap-rst-zhPDF_by_rst.png}}
  1. [[attachment:usage-py-reportlab-rst-zhPDF_by_OOo2.pdf]]
   * {{attachment:snap-rst-zhPDF_by_OOo2.png}}
  1. [[attachment:usage-py-reportlab-rst-zhPDF_by_firefox.pdf]]
   * {{attachment:snap-rst-zhPDF_by_firefox.png}}
Line 133: Line 133:
创建 by -- ZoomQuiet [[[DateTime(2008-08-08T07:43:16Z)]]]
||<^>[[PageComment2]]||<^>[:/PageCommentData:PageCommentData]''||
创建 by -- ZoomQuiet [<<DateTime(2008-08-08T07:43:16Z)>>]

文用 Reportlab 生成中文 PDF

Jiahua Huang <[email protected]>
reply-to        [email protected]
to      "Python.cn@google" <[email protected]>
date    Fri, Aug 8, 2008 at 15:13

subject [CPyUG:61808] 简单使用 Reportlab 生成中文 PDF 文档

  • 再次看到关于将 reStructurctedText、asciidoc、xhtml 转换为 PDF 格式的话题,
  • python 里用 Reportlab 应该比用 tex、docbook、fop 合适,

不过 python-reportlab 默认没中文字体,也没中文换行,

  • 而现有的使用 Reportlab 的程序改起来也麻烦,

便干脆用运行中修改的方式覆盖掉 reportlab.lib.fonts.ps2tt reportlab.lib.fonts.tt2ps reportlab.platypus.Paragraph.wrap 让强制用中文字体,强制尝试中文换行(,强制用 UTF-8 中文)

  • 好处是现有程序和库 (比如现有的那堆 rst2pdf、html2pdf、wiki2pdf 等程序) 不用做任何改动就可以中文,

缺陷是连英文也没粗体斜体了。

实例和分析

  • html2pdf.7z

  • 最新版本:html2pdf @ openbookproject.googlecode.com

  • 添加
    • ## Hack for Chinese fonts ############################

   1 import reportlab.rl_config
   2 reportlab.rl_config.warnOnMissingFontGlyphs = 0
   3 import reportlab.pdfbase.pdfmetrics
   4 import reportlab.pdfbase.ttfonts
   5 reportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('song',
   6 '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttf'))
   7 import reportlab.lib.fonts
   8 reportlab.lib.fonts.ps2tt = lambda psfn: ('song', 0, 0)
   9 reportlab.lib.fonts.tt2ps = lambda fn,b,i: 'song'
  10 ## for CJK Wrap
  11 import reportlab.platypus
  12 def wrap(self, availWidth, availHeight):
  13   # work out widths array for breaking
  14   self.width = availWidth
  15   leftIndent = self.style.leftIndent
  16   first_line_width = availWidth -
  17 (leftIndent+self.style.firstLineIndent) - self.style.rightIndent
  18   later_widths = availWidth - leftIndent - self.style.rightIndent
  19   try:
  20       self.blPara = self.breakLinesCJK([first_line_width, later_widths])
  21   except:
  22       self.blPara = self.breakLines([first_line_width, later_widths])
  23   self.height = len(self.blPara.lines) * self.style.leading
  24   return (self.width, self.height)
  25 
  26 reportlab.platypus.Paragraph.wrap = wrap
  27 
  28 import sys
  29 reload(sys)
  30 sys.setdefaultencoding('utf8')

追加 pisa3 中文技巧

使用 pisa3 html2pdf 前插入下边语句

   1 ## Hack for Chinese fonts ############################
   2 import reportlab.rl_config
   3 reportlab.rl_config.warnOnMissingFontGlyphs = 0
   4 from reportlab.pdfbase import pdfmetrics
   5 from reportlab.pdfbase.ttfonts import TTFont
   6 
   7 from reportlab.pdfgen import canvas
   8 pdfmetrics.registerFont(TTFont('song', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttf'))
   9 from reportlab.lib import fonts
  10 
  11 fonts.ps2tt = lambda psfn: ('song', 0, 0)
  12 fonts.tt2ps = lambda fn,b,i: 'song'
  13 
  14 import reportlab.platypus
  15 def wrap(self, availWidth, availHeight):
  16 # work out widths array for breaking
  17 self.width = availWidth
  18 leftIndent = self.style.leftIndent
  19 first_line_width = availWidth - (leftIndent+self.style.firstLineIndent) - self.style.rightIndent
  20 later_widths = availWidth - leftIndent - self.style.rightIndent
  21 try:
  22 self.blPara = self.breakLinesCJK([first_line_width, later_widths])
  23 except:
  24         #print 'breakLinesCJK except:',self, availWidth, availHeight
  25 
  26 self.blPara = self.breakLines([first_line_width, later_widths])
  27 self.height = len(self.blPara.lines) * self.style.leading
  28 return (self.width, self.height)
  29 
  30 reportlab.platypus.Paragraph.wrap = wrap
  31 
  32 ## no zoomed for bookmarkPage
  33 import reportlab.pdfgen.canvas
  34 bookmarkPage = reportlab.pdfgen.canvas.Canvas.bookmarkPage
  35 
  36 reportlab.pdfgen.canvas.Canvas.bookmarkPage = lambda self, key, **kwargs: apply(bookmarkPage, (self, key), {'fit':'XYZ'})
  37 
  38 ## for image
  39 import pisa_default; pisa_default.TAGS["img"][1]["src"] = (8, 23) #(STRING, MUST)
  40 
  41 
  42 import sys
  43 reload(sys)
  44 sys.setdefaultencoding('utf8')
  45 ########################################################

使用和对比


反馈

创建 by -- ZoomQuiet [2008-08-08 07:43:16]

MiscItems/2008-08-08 (last edited 2009-12-25 07:09:01 by localhost)