mPDF Manual – Paging

Page numbering

{PAGENO} in a header or footer will be replaced by the document page number. By default this will be the actual page number in decimal numerals.

You can optionally:

  • suppress page numbering - {PAGENO} will be replaced by a blank string - but counting will continue
  • reset numbering - reset the numbering anywhere in the document
  • define/change the style of the numbering - as for lists it can be roman, alphabetical or decimal

From mPDF ≥ 3.0 the page numbering can be reset to any positive number. Prior to this, it was only possible to reset it to 1.

From mPDF 6.0 the page number style can include any of the values used for list-style-type.

Page numbering changes are made when adding a new page using AddPage() or <pagebreak> (or TOCpagebreak() and  <tocpagebreak>) or using @page.

You could obviously also hide the page numbering by redefining a header or footer that does not include '{PAGENO}'.

Example #1

<?php
$mpdf = new \Mpdf\Mpdf();

// Set a simple Footer including the page number
$mpdf->setFooter('{PAGENO}');

$mpdf->WriteHTML('Section 1');
$mpdf->WriteHTML('');

// You could also do this using
// $mpdf->AddPage('','','','','on');

$mpdf->WriteHTML('Section 2 - No Footer');
$mpdf->WriteHTML('<pagebreak resetpagenum="1" pagenumstyle="a" suppress="off" />');
$mpdf->WriteHTML('Section 3 - Starting with page a');

$mpdf->Output();

Changing page numbering from the start of the document

If you want to set page numbering characteristics from the first page onwards, you should explicitly add the first page of the document using AddPage(). Note that this is normally not required, as mPDF creates a new first page automatically if required when first using WriteHTML().

Example #2

In a complex document you could suppress the page numbering from the start for the cover page(s); then reset the number to 1, and set the style as lowercase Roman (i, ii, iii) for the foreword and introduction; then reset the style to decimal for the rest of the document:

<?php
$mpdf = new \Mpdf\Mpdf();

// Double-side document - mirror margins
$mpdf->mirrorMargins = 1;

// Set a simple Footer including the page number
$mpdf->setFooter('{PAGENO}');

// Turn off (suppress) page numbering from the start of the document
$mpdf->AddPage('','','','','on');

$mpdf->WriteHTML('Your Front Cover Pages');

// You could also do this using
// $mpdf->AddPage('','NEXT-ODD','1','i','off');
$mpdf->WriteHTML('Your Foreword and Introduction');
$mpdf->WriteHTML('<pagebreak type="NEXT-ODD" pagenumstyle="1" />');
$mpdf->WriteHTML('Your Book text');

$mpdf->Output();


If using the initial  AddPage() causes problems (a blank first page), an alternative way to do this is:

<?php
$mpdf->PageNumSubstitutions[] = [
    'from' => 1,
    'reset' => 0,
    'type' => 'I',
    'suppress' => 'on'
];

See Also

  • defaultPageNumStyle - Specify a default page numbering style from the start of the document
  • pagenumPrefix - Specify text to precede page numbers generated by {PAGENO}
  • pagenumSuffix - Specify text to follow page numbers generated by {PAGENO}
  • nbpgPrefix - Specify text to precede page total generated by {nbpg}
  • nbpgSuffix - Specify text to follow page numbers generated by {nbpg}
  • Replaceable aliases
  • aliasNbPg - Specify the text to be replaced by the document page total
  • aliasNbPgGp - Specify the text to be replaced by the group page total

Please also see Table of Contents for this special case.

Fork me on GitHub