Contents
reportlab生成pdf的中文自动换行
He Jibo <[email protected]> sender-time Sent at 00:58 (GMT-04:00). Current time there: 10:13 PM. ✆ reply-to [email protected] to python-chinese <[email protected]> date Fri, Jul 9, 2010 at 00:58 subject [CPyUG] 用reportlab 生成中文pdf时,如何实现中文的自动换行?
我现在正在用reportlab写个东西的。 > > 就是如果设置了一个段落的首行缩进两个字,那么后面的每行的末尾都有空白,估计是行的长度计算错误,能否看看? > > > 还有就是因为文件如果过大,有个def progressCB(typ, value):,看了半天不知道怎么用。。。请指点一下,谢谢哦。
zi w
zi w <[email protected]> reply-to [email protected] to python-cn`CPyUG`华蟒用户组 <[email protected]> date Fri, Jul 9, 2010 at 08:42
网上找的,看这里
代码
Django 利用 reportlab 生成中文 PDF
- Django 可以通过 reportlab 生成pdf,并以附件的形式返回给客户端,但是 reportlab 生成中文的pdf还需要字体和换行的设置。具体例子如下:
1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3
4 from django.http import HttpResponse
5 from cStringIO import StringIO
6 from reportlab.pdfgen import canvas
7 from reportlab import rl_config
8 from reportlab.pdfbase import pdfmetrics
9 from reportlab.pdfbase.ttfonts import TTFont
10 from reportlab.lib.units import inch
11 from reportlab.lib.utils import simpleSplit
12 from reportlab.lib.styles import getSampleStyleSheet
13 from reportlab.platypus import Paragraph
14 from reportlab.lib.fonts import addMapping
15
16 def hello_pdf(request):
17 rl_config.warnOnMissingFontGlyphs = 0
18 pdfmetrics.registerFont(TTFont('song', '/home/yisl04/.fonts/simsun.ttc'))
19 pdfmetrics.registerFont(TTFont('fs', '/home/yisl04/.fonts/simfang.ttf'))
20 pdfmetrics.registerFont(TTFont('hei', '/home/yisl04/.fonts/simhei.ttf'))
21 pdfmetrics.registerFont(TTFont('yh', '/home/yisl04/.fonts/msyh.ttf'))
22
23 #设置字体:常规、斜体、粗体、粗斜体
24 addMapping('cjk', 0, 0, 'song') #normal
25 addMapping('cjk', 0, 1, 'fs') #italic
26 addMapping('cjk', 1, 0, 'hei') #bold
27 addMapping('cjk', 1, 1, 'yh') #italic and bold
28
29 response = HttpResponse(mimetype='application/pdf')
30 response['Content-Disposition'] = 'attachment; filename=hello.pdf'
31
32 temp = StringIO()
33 p = canvas.Canvas(temp)
34
35 #默认(0, 0)点在左下角,此处把原点(0,0)向上和向右移动,后面的尺寸都是相对与此原点设置的
36 #注意:移动原点时,向右向上为正,坐标系也是向右为+x,向上为+y
37 p.translate(0.5*inch, 0.5*inch)
38 #设置字体
39 p.setFont('song', 16)
40 #设置颜色,画笔色和填充色
41 p.setStrokeColorRGB(0.2, 0.5, 0.3)
42 p.setFillColorRGB(1, 0, 1)
43 #画一个矩形
44 p.rect(0, 0, 3*inch, 3*inch, fill=1)
45 #旋转文字方向
46 p.rotate(90)
47 p.setFillColorRGB(0, 0, 0.77)
48 p.drawString(3*inch, -3*inch, u"我是吴仁智,呵呵!")
49 p.rotate(-90)
50 p.setFont('yh', 16)
51 p.drawString(0, 0, u"drawString默认不换行!")
52 #插入图片
53 p.drawImage("/home/yisl04/public_html/yisl04.png", 5*inch, 5*inch, inch, inch)
54 #设置drawString最大宽度
55 L = simpleSplit(u'simpleSplit 只能用于 drawString 英文断行。', 'yh', 16, 9*inch)
56 y = 9*inch
57 for t in L:
58 p.drawString(0, y, t)
59 y -= p._leading
60
61 #Paragraph下中文断行(网上摘抄)
62 def wrap(self, availWidth, availHeight):
63 # work out widths array for breaking
64 self.width = availWidth
65 leftIndent = self.style.leftIndent
66 first_line_width = availWidth - (leftIndent+self.style.firstLineIndent) - self.style.rightIndent
67 later_widths = availWidth - leftIndent - self.style.rightIndent
68 try:
69 self.blPara = self.breakLinesCJK([first_line_width, later_widths])
70 except:
71 self.blPara = self.breakLines([first_line_width, later_widths])
72 self.height = len(self.blPara.lines) * self.style.leading
73 return (self.width, self.height)
74 Paragraph.wrap = wrap
75
76 #中文断行还可以使用下面这种简单的方法
77 #from reportlab.lib.styles import ParagraphStyle
78 #ParagraphStyle.defaults['wordWrap']="CJK"
79
80 styleSheet = getSampleStyleSheet()
81 style = styleSheet['BodyText']
82 style.fontName = 'song'
83 style.fontSize = 16
84 #设置行距
85 style.leading = 20
86 #首行缩进
87 style.firstLineIndent = 32
88 Pa = Paragraph(u'<b>这里是粗体</b>,<i>这里是斜体</i>, <strike>这是删除线</strike>, <u>这是下划线</u>, <sup>这是上标</sup>, <em>这里是强调</em>, <font color=#ff0000>这是红色</font>', style)
89
90 Pa.wrapOn(p, 6*inch, 8*inch)
91 Pa.drawOn(p, 0, 5*inch)
92
93 p.showPage()
94 p.save()
95 response.write(temp.getvalue())
96 return response
反馈
创建 by -- ZoomQuiet [2010-07-09 02:16:00]