Django 利用 reportlab 生成中文 PDF
-
Django 可以通过 reportlab 生成pdf,并以附件的形式返回给客户端,但是 reportlab 生成中文的pdf还需要字体和换行的设置。具体例子如下:
from django.http import HttpResponse
from cStringIO import StringIO
from reportlab.pdfgen import canvas
from reportlab import rl_config
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import inch
from reportlab.lib.utils import simpleSplit
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph
from reportlab.lib.fonts import addMapping
def hello_pdf(request):
rl_config.warnOnMissingFontGlyphs = 0
pdfmetrics.registerFont(TTFont('song', '/home/yisl04/.fonts/simsun.ttc'))
pdfmetrics.registerFont(TTFont('fs', '/home/yisl04/.fonts/simfang.ttf'))
pdfmetrics.registerFont(TTFont('hei', '/home/yisl04/.fonts/simhei.ttf'))
pdfmetrics.registerFont(TTFont('yh', '/home/yisl04/.fonts/msyh.ttf'))
addMapping('cjk', 0, 0, 'song')
addMapping('cjk', 0, 1, 'fs')
addMapping('cjk', 1, 0, 'hei')
addMapping('cjk', 1, 1, 'yh')
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=hello.pdf'
temp = StringIO()
p = canvas.Canvas(temp)
p.translate(0.5*inch, 0.5*inch)
p.setFont('song', 16)
p.setStrokeColorRGB(0.2, 0.5, 0.3)
p.setFillColorRGB(1, 0, 1)
p.rect(0, 0, 3*inch, 3*inch, fill=1)
p.rotate(90)
p.setFillColorRGB(0, 0, 0.77)
p.drawString(3*inch, -3*inch, u"我是吴仁智,呵呵!")
p.rotate(-90)
p.setFont('yh', 16)
p.drawString(0, 0, u"drawString默认不换行!")
p.drawImage("/home/yisl04/public_html/yisl04.png", 5*inch, 5*inch, inch, inch)
L = simpleSplit(u'simpleSplit 只能用于 drawString 英文断行。', 'yh', 16, 9*inch)
y = 9*inch
for t in L:
p.drawString(0, y, t)
y -= p._leading
def wrap(self, availWidth, availHeight):
self.width = availWidth
leftIndent = self.style.leftIndent
first_line_width = availWidth - (leftIndent+self.style.firstLineIndent) - self.style.rightIndent
later_widths = availWidth - leftIndent - self.style.rightIndent
try:
self.blPara = self.breakLinesCJK([first_line_width, later_widths])
except:
self.blPara = self.breakLines([first_line_width, later_widths])
self.height = len(self.blPara.lines) * self.style.leading
return (self.width, self.height)
Paragraph.wrap = wrap
styleSheet = getSampleStyleSheet()
style = styleSheet['BodyText']
style.fontName = 'song'
style.fontSize = 16
style.leading = 20
style.firstLineIndent = 32
Pa = Paragraph(u'<b>这里是粗体</b>,<i>这里是斜体</i>, <strike>这是删除线</strike>, <u>这是下划线</u>, <sup>这是上标</sup>, <em>这里是强调</em>, <font color=#ff0000>这是红色</font>', style)
Pa.wrapOn(p, 6*inch, 8*inch)
Pa.drawOn(p, 0, 5*inch)
p.showPage()
p.save()
response.write(temp.getvalue())
return response
转自:http://wiki.woodpecker.org.cn/moin/MiscItems/2010-07-09
Django 利用 reportlab 生成中文 PDF Django 可以通过 reportlab 生成pdf,并以附件的形式返回给客户端,但是 reportlab 生成中文的pdf还需要字体和换行的设置。具体例子如下: #!/usr/bin/python# -*- coding:utf-8 -*-from django.http import HttpResponsefro
Python
reportlab
之 draw函数介绍
本节我们讲介绍一下操作
canvas
时经常用的工具API。在后续
教程
中
我将会对每个工具进行详细介绍,本文把他们都介绍给大家,方便朋友们开发之用。
Line methods
canvas
.line(x1,y1,x2,y2)
canvas
.lines(linelist)
用line和lines方法可以直接...
昨天,就在昨天,咱用了用
ReportLab
,那效果真的好得不得了,可惜因为篇幅问题,只是小刀牛试了一把,在解决了它的
中文
问题后,就戛然而止了,甚是寡淡无味。
为了让
Python
3处理
pdf
的口味更重一些,咱就来给
ReportLab
添点料。
这次要实现的功能,主要是标题和一段文字的描述,这么有用的功能就问你惊不惊喜,意不...
Reportlab
是一个用于
生成
PDF
文件的
Python
库。要
生成
具有目录和标签的
PDF
,需要使用
Reportlab
的相关功能来构建文档的结构和样式。
下面是一个示例代码,可以帮助您创建带有目录和标签的
PDF
文件:
from
reportlab
.lib import styles
from
reportlab
.lib.units import cm
from
reportlab
.platypus import SimpleDocTemplate, Paragraph, Spacer, TableOfContents
# 文档布局
doc = SimpleDocTemplate("mydocument.
pdf
")
style = styles["Normal"]
toc = TableOfContents()
toc.levelStyles = [
styles.Heading1,
styles.Heading2,
styles.Heading3
# 页面布局
story = []
story.append(toc)
story.append(Spacer(1, 0.5*cm))
story.append(Paragraph("Chapter 1", styles.Heading1))
story.append(Paragraph("Section 1.1", styles.Heading2))
story.append(Paragraph("This is the text of section 1.1", style))
story.append(Paragraph("Chapter 2", styles.Heading1))
story.append(Paragraph("Section 2.1", styles.Heading2))
story.append(Paragraph("This is the text of section 2.1", style))
# 渲染文档
doc.build(story, onFirstPage=toc.updatePageCallback, onLaterPages=toc.updatePageCallback)
上面代码
中
,
- 首先, 使用 SimpleDocTemplate 创建一个
PDF
文档对象
- 然后, 通过TableOfContents()来建立文档目录
- 然后, 通过Paragraph()来建立不同的标题和内容
- 最后, 通过doc.build()函数来渲染文档
这是一个简单的例子,您可以根据自己的需求进行更深入的调整和定制。