Showing page count with ReportLab

荒凉一梦 提交于 2019-11-29 01:37:38

I was able to implement the NumberedCanvas approach from ActiveState. It was very easy to do and did not change much of my existing code. All I had to do was add that NumberedCanvas class and add the canvasmaker attribute when building my doc. I also changed the measurements of where the "x of y" was displayed:

self.doc.build(pdf)

became

self.doc.build(pdf, canvasmaker=NumberedCanvas) 

doc is a BaseDocTemplate and pdf is my list of flowable elements.

use doc.multiBuild

and in the page header method (defined by "onLaterPages="):

global TOTALPAGES
if doc.page > TOTALPAGES:
    TOTALPAGES = doc.page
else:
    canvas.drawString(270 * mm, 5 * mm, "Seite %d/%d" % (doc.page,TOTALPAGES))

Just digging up some code for you, we use this:

SimpleDocTemplate(...).build(self.story,
                             onFirstPage=self._on_page,
                             onLaterPages=self._on_page)

Now self._on_page is a method that gets called for each page like:

def _on_page(self, canvas, doc):
    # ... do any additional page formatting here for each page
    print doc.page
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!