python - 有人可以给我使用reportlab生成带有页眉(添加图像)、页脚(Y页X)和带有书签的目录的PDF报告吗?
问题描述
我可以生成带有页眉和页脚的 PDF,也可以生成带有目录的 PDF,但是我不能生成带有页眉(图像)、页脚(y 的 x 页)和带有书签的目录的 pdf,我已经花了好几天把这三个功能结合在一起,但是失败了,谁能给我一些建议?我的代码有什么问题,未显示 pdf 的目录,并说“目录 0 的占位符”
class MyDocTemplate(BaseDocTemplate):
def __init__(self, filename, **kw):
self.allowSplitting = 0
BaseDocTemplate.__init__(self, filename, **kw)
template = PageTemplate('normal', [Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm)])
self.addPageTemplates(template)
def afterFlowable(self, flowable):
if isinstance(flowable, Paragraph):
txt = flowable.getPlainText()
style = flowable.style.name
if style == 'Heading1':
key = 'h1-%s' % self.seq.nextf('heading1')
self.canv.bookmarkPage(key)
self.notify('TOCEntry', (0, txt, self.page))
elif style == 'Heading2':
key = 'h2-%s' % self.seq.nextf('heading2')
print (key)
self.canv.bookmarkPage(key)
self.notify('TOCEntry', (1, txt, self.page, key))
class NumberedCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
canvas.Canvas.__init__(self, *args, **kwargs)
self._saved_page_states = []
def showPage(self):
self._saved_page_states.append(dict(self.__dict__))
self._startPage()
def save(self):
"""add page info to each page (page x of y)"""
num_pages = len(self._saved_page_states)
for state in self._saved_page_states:
self.__dict__.update(state)
self.draw_page_number(num_pages)
canvas.Canvas.showPage(self)
canvas.Canvas.save(self)
def draw_page_number(self, page_count):
self.setFont('Times-Bold',14)
self.drawRightString(7.6*inch,.5*inch,
"Page %d of %d" % (self._pageNumber, page_count))
if __name__ == "__main__":
h1 = PS(name = 'Heading1',
fontSize = 14,
leading = 16)
h2 = PS(name = 'Heading2',
fontSize = 12,
leading = 14,
leftIndent = 25)
#Build story.
story = []
toc = TableOfContents()
#For conciseness, using the same styles for headings and TOC entries
toc.levelStyles = [h1, h2]
story.append(toc)
story.append(PageBreak())
story.append(Paragraph('First heading', h1))
story.append(Paragraph('Text in first heading', PS('body')))
story.append(Paragraph('First sub heading', h2))
story.append(Paragraph('Text in first sub heading', PS('body')))
story.append(PageBreak())
story.append(Paragraph('Second sub heading', h2))
story.append(Paragraph('Text in second sub heading', PS('body')))
story.append(Paragraph('Last heading', h1))
doc = MyDocTemplate("mypdf.pdf")
doc.multiBuild(story, canvasmaker=NumberedCanvas)
解决方案
检查您的代码缩进。该afterFlowable
方法应在内部定义class MyDocTemplate
:
class MyDocTemplate(BaseDocTemplate):
def __init__(self, filename, **kw):
self.allowSplitting = 0
BaseDocTemplate.__init__(self, filename, **kw)
template = PageTemplate('normal', [Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm)])
self.addPageTemplates(template)
def afterFlowable(self, flowable):
if isinstance(flowable, Paragraph):
txt = flowable.getPlainText()
style = flowable.style.name
...
推荐阅读
- javascript - 如何使用reduce输入多维累加器?
- bots.business - 使用 Bot 业务从 sendPoll 电报 bot 获取投票结果
- jenkins - Graylog:如何将 WildFly 和 Jenkins 服务器日志发送(连接)到 Graylog
- google-assistant - Android 设备上的 Mediaplayer 直播质量问题
- ios - ios 14:NSOSStatusErrorDomain -25293 尝试访问钥匙串并使用真实设备
- outlook - 使用扩展属性字段 (EWS) 在 EmailMessage 中创建投票
- automation - 如何迭代查询参数(用于 API)
- reactjs - 交错:如何回到初始位置
- javascript - Nextjs monorep - 存储 API 调用的 URL 的位置
- javascript - 有没有办法使用 JavaScript 来确定图像的高度/宽度并调整它的大小?