Here's an example of how to create a footer with page numbers using ReportLab in Python. This example uses the canvas.Canvas class and a custom page template to add footers:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
def create_pdf_with_footer(output_filename):
# Create a new PDF document
c = canvas.Canvas(output_filename, pagesize=letter)
# Define the footer function
def add_footer(canvas, doc):
# Get page dimensions
width, height = letter
# Save the state of the canvas
canvas.saveState()
# Set font for footer
canvas.setFont('Helvetica', 10)
# Create footer text with page number
footer_text = f"Page {canvas.getPageNumber()}"
# Position the footer (centered, 0.5 inch from bottom)
canvas.drawCentredString(width/2, 0.5*inch, footer_text)
# Restore the state
canvas.restoreState()
# Add some sample content to demonstrate multiple pages
for i in range(5): # Create 5 pages
# Add some sample text
c.setFont('Helvetica', 12)
c.drawString(1*inch, 10*inch, f"This is page {i+1} of the document")
c.drawString(1*inch, 9.5*inch, "Here is some sample content...")
# Add the footer
add_footer(c, None)
# Start a new page (except for the last iteration)
if i < 4:
c.showPage()
# Save the PDF
c.save()
# Create the PDF
create_pdf_with_footer("document_with_footer.pdf")
If you're using ReportLab's SimpleDocTemplate (which is often more convenient for complex documents), here's an alternative approach:
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
def create_pdf_with_footer_using_template(output_filename):
# Create the PDF document
doc = SimpleDocTemplate(output_filename, pagesize=letter)
styles = getSampleStyleSheet()
# Container for the 'Flowable' objects
elements = []
# Add some sample content
for i in range(50): # Create enough content for multiple pages
elements.append(Paragraph(f"Paragraph {i+1}", styles['Normal']))
elements.append(Spacer(1, 0.2*inch))
# Define the footer function
def add_page_number(canvas, doc):
page_num = canvas.getPageNumber()
text = f"Page {page_num}"
canvas.saveState()
canvas.setFont('Helvetica', 10)
canvas.drawCentredString(letter[0]/2, 0.5*inch, text)
canvas.restoreState()
# Build the PDF with the footer
doc.build(elements, onFirstPage=add_page_number, onLaterPages=add_page_number)
# Create the PDF
create_pdf_with_footer_using_template("document_with_footer_template.pdf")
Key points about these examples:
-
The first example uses the basic canvas.Canvas approach:
- We define a separate
add_footer function that handles the footer drawing
- We call this function manually for each page
- We use
canvas.getPageNumber() to get the current page number
-
The second example uses SimpleDocTemplate:
- It's more suitable for documents with flowing content
- The footer function is passed to the
build method using onFirstPage and onLaterPages
- This approach allows different footers for first vs. later pages if needed
-
Common features in both examples:
- The footer is centered 0.5 inches from the bottom
- Uses Helvetica font at 10pt for the footer
- Uses
saveState() and restoreState() to prevent footer drawing from affecting other content
To customize either example, you could:
- Change the footer position by modifying the x, y coordinates in
drawCentredString
- Change the font or font size by modifying the
setFont call
- Add more footer content (like dates, document title, etc.)
- Add lines or other graphics to the footer
Here's how you might add more footer content:
def add_footer_with_more_info(canvas, doc):
canvas.saveState()
canvas.setFont('Helvetica', 10)
# Left-aligned text
canvas.drawString(0.5*inch, 0.5*inch, "My Company Name")
# Centered page number
page_num = canvas.getPageNumber()
canvas.drawCentredString(letter[0]/2, 0.5*inch, f"Page {page_num}")
# Right-aligned date
from datetime import date
canvas.drawRightString(letter[0]-0.5*inch, 0.5*inch, str(date.today()))
# Optional: Add a line above the footer
canvas.line(0.5*inch, 0.75*inch, letter[0]-0.5*inch, 0.75*inch)
canvas.restoreState()
Choose the approach that best fits your needs:
- Use the first approach (
canvas.Canvas) for simple documents or when you need precise control over layout
- Use the second approach (
SimpleDocTemplate) for documents with flowing text and complex content