Headers & Footers
Page headers and page footers can be set for mPDF documents in a number of different ways. As mPDF has evolved, new methods have been added.
Warning: Use of CSS @page selector with any content will rewrite/remove headers not defined in the selector. See @page selector documentation.
Note: As from mPDF v6.0 Headers and Footers are all now written internally as HTMLheaders/footers.
The use of non-HTML headers and footers (methods 1 & 3) is deprecated, but remains supported. Non-HTML headers and footers are converted in mPDF to HTML equivalents.
Default non-HTML headers will not clash with HTML headers, but named non-HTML headers WILL clash with
(and overwrite) HTML headers of the same (equivalent) name
e.g. MyFooter
(non-HTML) overwrites html_MyFooter
(HTML).
Note: As from mPDF v7.1 Headers and Footers support absolute positioning, however you cannot use both absolute and non-absolute positioned content together in the Headers and Footers.
Types
- RUNTIME
- RUNTIME headers/footers are set as they are required throughout the document.
- NAMED
- NAMED headers/footers are defined at any time (before they are used) and given a ‘name’; they can then be used/re-used by reference to that name at any time. They can be defined by either PHP script or custom HTML tags.
- Non-HTML
- (deprecated) NON-HTML headers/footers are set by text strings with no HTML mark up to define styles. Style changes (font-size, font-family, color) can be defined by changing mPDF variables.
- HTML
- HTML headers/footers are written in standard HTML code. They can only be
defined outside HTML block tags (except
<body>
).
Note: HTML headers/footers are more intensive of processing time and memory. This probably makes little difference for an average/short document, but may be important for long/complex documents.
Methods
NON-HTML | HTML | ||
RUNTIME |
Method 1 (deprecated) This is the simplest & quickest way to define a header/footer for the whole document if you need limited control over styling. SetHeader() SetFooter() 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. Style can be defined by variables: $defaultheaderfontsize $defaultheaderfontstyle $defaultheaderline $defaultfooterfontsize $defaultfooterfontstyle $defaultfooterline |
The simplest & quickest way to program a header/footer once for the whole document that includes images or uses more complex layout styles. |
|
NAMED | Define |
Method 3 (deprecated) This method is useful if you do not need the flexibility of an HTML header/footer, but are changing headers/footers throughout the document. DefHeaderByName() DefFooterByName() <pageheader> <pagefooter> |
This is the best way for complex headers/footers with the advantage of HTML code, but you can easily change the headers/footers at any time during the document. DefHTMLHeaderByName() DefHTMLFooterByName() <htmlpageheader> <htmlpagefooter> |
Reference |
These methods are recommended when setting the header/footer at the start of a document. SetHeaderByName() SetFooterByName() <setpageheader> <setpagefooter> @page |
These methods are recommended when setting the header/footer at the start of a document. SetHTMLHeaderByName() SetHTMLFooterByName() <sethtmlpageheader> <sethtmlpagefooter> @page |
|
Recommended when you wish to change the headers/footers during the document. These methods can reference any NAMED header or footer (NON-HTML or HTML) |
In any of the options, {PAGENO}
or {DATE j-m-Y}
can be used - which will be replaced by the page number or current date.
j-m-Y
can be replaced by any of the valid formats used in the PHP date() function.
See also
- Method 1 RUNTIME non-HTML headers & footers (deprecated)
- Method 2 RUNTIME HTML headers & footers
- Method 3 NAMED non-HTML headers & footers (deprecated)
-
Method 4 NAMED HTML headers & footers
- forcePortraitHeaders