[reportlab-users] Get Total Page Count

Tim Roberts timr at probo.com
Tue Nov 27 16:57:09 EST 2018


Mike Hostetler wrote:
> Next newbie question...
>
> How can I get the total page count of the document to be in the PDF? I 
> need a footer with "Page x of Total". Will I have to do a "pageCallback"?

It's trickier than that.  ReportLab does not construct the whole 
document in advance, so it doesn't know the total page count at the 
point it is completing page 1.

There are web sites that describe the process.  Some of them just render 
twice and fill it in the second time, but it's easier to use a "form", 
which is a block of PDF that can be referred to multiple times.  You'd say

     canvas.doForm("PageCount")

in your footer, then at the end of the document you'd fill out the form:

     canvas.beginForm("PageCount")
     canvas.drawString( 0, 0, str(total_pages), ... )
     canvas.endForm()

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list