Headers, footers and page numbers
Written for mPDF 8.2 on PHP 8.1+. Updated 2 November 2026.
mPDF repeats headers and footers on every page and fills in page numbers as it lays out the document.
The quick way
$mpdf->SetHTMLHeader('<div style="text-align: right; font-size: 9pt;">Acme Ltd · Quarterly report</div>');
$mpdf->SetHTMLFooter('<div style="text-align: center; font-size: 9pt;">Page {PAGENO} of {nbpg}</div>');
$mpdf->WriteHTML($body);
Call these before WriteHTML() so they apply from the first page. Make sure margin_top and margin_bottom are large enough to hold them. Otherwise the body runs underneath.
Placeholders
| Placeholder | Becomes |
|---|---|
{PAGENO} |
The current page number. |
{nbpg} |
The total number of pages. |
{DATE j F Y} |
Today’s date, using PHP date() letters, e.g. 2 November 2026. |
For a plain three-part footer, SetFooter('Left text|{PAGENO}|Right text') splits on the | characters into left, centre and right.
Named headers in the HTML
To keep everything in the template, define headers and footers as tags and attach them with @page:
<htmlpageheader name="main">
<div style="border-bottom: 1px solid #999; font-size: 9pt;">Acme Ltd</div>
</htmlpageheader>
<htmlpagefooter name="main">
<div style="text-align: right; font-size: 9pt;">{PAGENO} / {nbpg}</div>
</htmlpagefooter>
<style>
@page { header: html_main; footer: html_main; margin-top: 25mm; margin-bottom: 20mm; }
</style>
Note the html_ prefix when you refer to the name. Different first pages, odd and even pages, and chapter pages are handled by naming more headers and switching with @page :first or named pages.
No header on the cover
Define a named page with no header for the cover, or start numbering after it:
<pagebreak resetpagenum="1" />
resetpagenum restarts {PAGENO} at the given number, which is useful when the cover and contents shouldn’t count. See page breaks.
Already have the PDF?
To number the pages of a finished file, without code, use Add page numbers, which runs in your browser.