mPDF Manual – Headers & Footers

Method 1

This uses RUNTIME NON-HTML headers & footers. This is the simplest & quickest way to define a header/footer for the whole document if you need limited control over styling. There are several variants of this method, using string or array. The simplest form does not allow different header/footer for ODD and EVEN pages.

Setting a Header/Footer at the start of a document

Variant #1 (Simplest form)

Use a single command with a string as parameter, to set a header/footer at the right margin of the page on ODD pages, and left margin for EVEN pages (if DOUBLE-SIDED document), or right margin for all pages.

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

$mpdf->SetHeader('Document Title');
$mpdf->WriteHTML('Document text');

$mpdf->Output();

Variant #2 (Split string)

Set a header/footer in three parts. The text string defines three strings divided by | which will set a header at the left/centre/right margin of the page on ODD pages and right/centre/left margin for EVEN pages (if  DOUBLE-SIDED document), or left/centre/right margin for all pages. Note the use of {PAGENO} which can be used in any type of header or footer.

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

$mpdf->SetHeader('Document Title|Center Text|{PAGENO}');
$mpdf->SetFooter('Document Title');
$mpdf->WriteHTML('Document text');

$mpdf->Output();

Variant #3 (Controlling style with variables)

This is the same as Variant #2, but you can control some aspects of style for the headers/footers by altering certain mPDF variables.

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

$mpdf->SetHeader('Document Title|Center Text|{PAGENO}');
$mpdf->SetFooter('Document Title');

$mpdf->defaultheaderfontsize=10;
$mpdf->defaultheaderfontstyle='B';
$mpdf->defaultheaderline=0;
$mpdf->defaultfooterfontsize=10;
$mpdf->defaultfooterfontstyle='BI';
$mpdf->defaultfooterline=0;

$mpdf->WriteHTML('Document text');
$mpdf->Output();

Variant #4 (Array) - DEPRECATED

Set a header/footer using an array of values. This allows greater control over styling. Recommended to use Variant #5, which is very similar, but specifies ODD and EVEN forms separately.

<?php

$arr = array (
  'odd' => array (
    'L' => array (
      'content' => '',
      'font-size' => 10,
      'font-style' => 'B',
      'font-family' => 'serif',
      'color'=>'#000000'
    ),
    'C' => array (
      'content' => '',
      'font-size' => 10,
      'font-style' => 'B',
      'font-family' => 'serif',
      'color'=>'#000000'
    ),
    'R' => array (
      'content' => 'My document',
      'font-size' => 10,
      'font-style' => 'B',
      'font-family' => 'serif',
      'color'=>'#000000'
    ),
    'line' => 1,
  ),
  'even' => array ()
);

$mpdf->SetHeader($arr);

Variant #5 (Array)

Set a header/footer using an array of values. This allows greater control over styling.  ODD and EVEN headers/footers are set separately using the second parameter of SetHeader().

<?php
$arr = array (
    'L' => array (
      'content' => 'Chapter 1',
      'font-size' => 10,
      'font-style' => 'B',
      'font-family' => 'serif',
      'color'=>'#000000'
    ),
    'C' => array (
      'content' => '',
      'font-size' => 10,
      'font-style' => 'B',
      'font-family' => 'serif',
      'color'=>'#000000'
    ),
    'R' => array (
      'content' => 'My document',
      'font-size' => 10,
      'font-style' => 'B',
      'font-family' => 'serif',
      'color'=>'#000000'
    ),
    'line' => 1,
);

$mpdf->SetHeader($arr, 'O');  // 'O' for Odd header

Although this looks complex, you could change one value easily throughout a document:

<?php
// following from above...
$mpdf->AddPage();

$arr['L']['content'] = 'Chapter 2';
$mpdf->SetHeader($arr, 'O');

Changing Header/Footer during the document

This is where RUNTIME headers/footers get much more clumsy to use, whichever of the Variants above you are using. When a new page is added to the document (e.g. using AddPage() or <pagebreak>) mPDF does the following:

  • writes the footer for the current page
  • starts the new page
  • writes the header for the new page

Therefore to use any RUNTIME method you need to:

  • change the header before the page-break
  • change the footer after the page-break
<?php
$mpdf = new \Mpdf\Mpdf();

$mpdf->SetHeader('First section header');
$mpdf->SetFooter('First section footer');
$mpdf->WriteHTML('First section text...');

// Set the new Header before you AddPage
$mpdf->SetHeader('Second section header');
$mpdf->AddPage();

// Set the new Footer after you AddPage
$mpdf->SetFooter('Second section footer');
$mpdf->WriteHTML('Second section text...');

$mpdf->Output();

It gets even more complicated if you are using DOUBLE-SIDED document and you want to start the new section of your book on an ODD page:

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

$mpdf->SetHeader('First section header');
$mpdf->SetFooter('First section footer');
$mpdf->WriteHTML('First section text...');

// Use a conditional page-break to ensure you are on an EVEN page before
// changing the Header
$mpdf->AddPage('','E');

// Now you know that this page-break takes you to an ODD page
$mpdf->SetHeader('Second section header');

$mpdf->AddPage();
$mpdf->SetFooter('Second section footer');
$mpdf->WriteHTML('Second section text...');

$mpdf->Output();

Table of Contents

Using RUNTIME headers/footers with a Table of Contents gets so clumsy, it is recommended that you consider one of the NAMED methods. Here for the record is how you would do it:

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

// Set header and footer arrays for section:Introduction
$intro_header_odd_array = array(...);
$intro_header_even_array = array(...);
$intro_footer_odd_array = array(...);
$intro_footer_even_array = array(...);

// Set header and footer arrays for section:Main
$main_header_odd_array = array(...);
$main_header_even_array = array(...);
$main_footer_odd_array = array(...);
$main_footer_even_array = array(...);

// Set the headers/footers for the Introduction
$mpdf->setHeader($intro_header_odd_array,'O');
$mpdf->setHeader($intro_header_even_array,'E');
$mpdf->setFooter($intro_footer_odd_array,'O');
$mpdf->setFooter($intro_footer_even_array,'E');

$mpdf->AddPage('', '', 1, '', 'on');    // suppress page numbering for the introduction
$mpdf->WriteHTML('Introduction of document...');

$mpdf->AddPage('','E');

$mpdf->setHeader($main_header_odd_array,'O');
$mpdf->setHeader($main_header_even_array,'E');

$mpdf->TOCpagebreak(
    '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
    $toc-preHTML, $toc-postHTML, $toc-bookmarkText,
    1, 'A', 'off'); // sets numbering to start at A

$mpdf->setFooter($main_footer_odd_array,'O');
$mpdf->setFooter($main_footer_even_array,'E');

$mpdf->WriteHTML('Main part of document...');

$mpdf->Output();

… and for historical purposes using deprecated TOC functions:

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

// Set header and footer arrays for section:Introduction
$intro_header_odd_array = array(...);
$intro_header_even_array = array(...);
$intro_footer_odd_array = array(...);
$intro_footer_even_array = array(...);

// Set header and footer arrays for section:Main
$main_header_odd_array = array(...);
$main_header_even_array = array(...);
$main_footer_odd_array = array(...);
$main_footer_even_array = array(...);

// Set the headers/footers for the Introduction
$mpdf->setHeader($intro_header_odd_array,'O');
$mpdf->setHeader($intro_header_even_array,'E');
$mpdf->setFooter($intro_footer_odd_array,'O');
$mpdf->setFooter($intro_footer_even_array,'E');

$mpdf->AddPage('', '', 1, '', 'on');    // suppress page numbering for the introduction

$mpdf->WriteHTML('Introduction of document...');

// Set some variables for the ToC - these are all now deprecated

$mpdf->TOCheader = array();
$mpdf->TOCfooter = array();
$mpdf->TOCpreHTML = '<h2>Table of Contents</h2>');
$mpdf->TOCpostHTML = 'Text to come after the contenst list';
$mpdf->TOCbookmarkText = 'Contents';

$mpdf->AddPage('','E');
$mpdf->setHeader($main_header_odd_array,'O');
$mpdf->setHeader($main_header_even_array,'E');

$mpdf->AddPage('', '', 1, 'i', 'off');    // sets page numbering to start here at 1
$mpdf->setFooter($main_footer_odd_array,'O');
$mpdf->setFooter($main_footer_even_array,'E');

$mpdf->TOC(
    $tocfont, $tocfontsize, $tocindent, $resetpagenum, $pagenumstyle,
    $suppress, $toc_orientation, $TOCusePaging, $TOCuseLinking
);

$mpdf->WriteHTML('Main part of document...');

$mpdf->Output();

See Also

Related to RUNTIME non-HTML headers & footers:

Fork me on GitHub