问题描述
在reportlab中具有页脚和页眉的最佳方法是什么,不仅是一行,而且可以在onPage函数中使用canvas.drawString进行绘制.找不到在onPage函数中将诸如Paragraph之类的内容放入页眉/页脚的方法.处理此问题的最佳方法是什么?有没有一种方法可以将段落放在页脚中?
What is a best way to have a footer and header in reportlab, that not just a single line, that can be drawed with canvas.drawString in onPage function. Didn`t find a way to put something like Paragraph into header/footer in onPage function. What is the best way to handle this? Is there a way to put a paragraph into footer ?
推荐答案
您可以在onPage函数中使用任意绘制命令,因此只需绘制一个段落即可(请参见reportlab用户指南).
You can use arbitrary drawing commands in the onPage function, so you can just draw a paragraph (see section 5.3 in the reportlab user guide) from your function.
这是一个完整的示例:
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate, Paragraph
styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading1']
def footer(canvas, doc):
canvas.saveState()
P = Paragraph("This is a multi-line footer. It goes on every page. " * 5,
styleN)
w, h = P.wrap(doc.width, doc.bottomMargin)
P.drawOn(canvas, doc.leftMargin, h)
canvas.restoreState()
doc = BaseDocTemplate('test.pdf', pagesize=letter)
frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height,
id='normal')
template = PageTemplate(id='test', frames=frame, onPage=footer)
doc.addPageTemplates([template])
text = []
for i in range(111):
text.append(Paragraph("This is line %d." % i,
styleN))
doc.build(text)
这篇关于reportlab中的多行(段落)页脚和页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!