[reportlab-users] page x of y using DocTemplate

Jade Meskill reportlab-users@reportlab.com
10 May 2002 14:28:13 -0700

On Fri, 2002-05-10 at 14:08, Jade Meskill wrote:
> On Fri, 2002-05-10 at 12:40, Andy Robinson wrote:
> > > I am trying to figure out how I can use a form to draw "Page x of y"
> > > with y being the last page drawn.  I understand how to create the form,
> > > and how to use the forward declaration, but I can't figure out how to
> > > tell when I'm on the last page of the document so I can use that page
> > > number as y.  Is there anything inside of the DocTemplate that will tell
> > > me when I'm on the last page?  Or any other way to get this
> > > information.  Once I call doc.build() the form must be created, but the
> > > pages haven't been generated yet.  Anybody have a suggestion? 
> > 
> > In our RML product (which is basically platypus translated into XML tags)
> > what we usually do is make the form declaration the very last thing 
> > in the 'story' part of the document.  So it gets the page number just 
> > once (using a tag we provide).   In Platypus you would need to put some 
> > custom flowable class at the end of the story which, when drawn, did not 
> > actually draw anything or consume space, but asked the canvas for the page 
> > number and then created a tiny little form with 'y' in it.
> > 
> > Let me know if that makes sense!  My brain is kind of fried from
> > a heavy week and I'm sure I could have worded it better :-)
> > 
> Ah, OK.  This makes sense to me.  I will try this out and post my
> results to the list.  Thanks for your help!

OK.  This ended up being much easier than I thought it would be.  Here
is some sample code for anybody else who is wondering how to do this.

Just append this to your story before doc.build() and it works like a

class lastPageNumberFlowable(Flowable):

    def __init__(self, xoffset = 0, yoffset = 0):
        self._xoffset = xoffset
        self._yoffset = yoffset
    def draw(self):
        canvas = self.canv
        if not canvas.hasForm("lastPageNumber"):
            canvas.setFont("Helvetica", 7)
            canvas.drawRightString(self._xoffset, self._yoffset,


Andy, Thanks again for your help!

Jade Meskill <jade.meskill@libertydistribution.com>
Liberty Distribution Company