Page Numbers and Page Breaks
from ironpdf import *

html = """
    <p> Hello Iron</p>
    <p> This is 1st Page </p>
    <div style='page-break-after: always;'></div>
    <p> This is 2nd Page</p>
    <div style='page-break-after: always;'></div>
    <p> This is 3rd Page</p>
"""

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Configure footer setting
footer = HtmlHeaderFooter()
footer.MaxHeight = 15  # millimeters
footer.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
footer.DrawDividerLine = True

renderer.RenderingOptions.HtmlFooter = footer

pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("pageNumber.pdf")
pip install ironpdf
Page Numbers and Page Breaks

Page Numbers and Page Breaks

IronPDF for Python supports automatic page numbering via HTML footers and explicit page breaks via CSS page-break-after directives, giving you precise control over how multi-page documents are structured.

Getting Started

Use the CSS property page-break-after: always in your HTML to force a new page at any point in the content. Pair this with an HtmlHeaderFooter footer configured with {page} and {total-pages} merge fields to automatically number every page.

Understanding the Code

  • page-break-after: always: A CSS rule placed inside a <div> that forces the content following it onto a new page. This is the standard way to create multi-page PDFs from a single HTML string.
  • HtmlHeaderFooter: An object that renders an HTML fragment as the footer on every page. Assign it to RenderingOptions.HtmlFooter.
  • HtmlFragment: The HTML content of the footer. In this example, {page} of {total-pages} is centered in italic text.
  • MaxHeight: Limits the footer area height in millimeters.
  • DrawDividerLine: When True, adds a horizontal separator above the footer.
  • RenderHtmlAsPdf(html): Renders the full multi-page HTML, applying the footer to each page automatically.

Available Page Merge Fields

{page} - current page number
{total-pages} - total number of pages in the document

Learn to create multi-page PDFs with IronPDF for Python!

Ready to Get Started?

Version:2026.11just released