With platypus (SimpleDocTemplate), you draw headers and footers in a page callback function, and use a custom canvas class to get the total page count.
from reportlab.pdfgen import canvas
class NumberedCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pages = []
def showPage(self):
self.pages.append(dict(self.__dict__))
self._startPage()
Then add a save method that loops through the stored pages, updates the canvas state for each, draws the footer text with drawRightString (for example "Page 2 of 10") using the length of self.pages, and calls the parent showPage and save. The ReportLab user guide and many examples show this "NumberedCanvas" pattern in full.
Build the document with doc.build(story, canvasmaker=NumberedCanvas). For a simple footer without the total count, pass onFirstPage and onLaterPages functions to build, and inside them call canvas.drawString at a fixed position near the bottom margin, using canvas.getPageNumber() for the current page.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
With platypus (SimpleDocTemplate), you draw headers and footers in a page callback function, and use a custom canvas class to get the total page count.
Then add a save method that loops through the stored pages, updates the canvas state for each, draws the footer text with drawRightString (for example "Page 2 of 10") using the length of self.pages, and calls the parent showPage and save. The ReportLab user guide and many examples show this "NumberedCanvas" pattern in full.
Build the document with doc.build(story, canvasmaker=NumberedCanvas). For a simple footer without the total count, pass onFirstPage and onLaterPages functions to build, and inside them call canvas.drawString at a fixed position near the bottom margin, using canvas.getPageNumber() for the current page.