Skip to content Skip to sidebar Skip to footer

Showing Page Count With Reportlab

I'm trying to add a simple 'page x of y' to a report made with ReportLab.. I found this old post about it, but maybe six years later something more straightforward has emerged? ^^;

Solution 1:

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.

Solution 2:

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))

Solution 3:

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 pageprint doc.page

Post a Comment for "Showing Page Count With Reportlab"