mPDF Manual – Headers & Footers

Headers & Top margins

(mPDF ≥ 4.0)

In standard usage, mPDF sets the following:

  • margin-top = distance in mm from top of page to start of text (ignoring any headers)
  • margin-header = distance in mm from top of page to start of header
  • margin-bottom = distance in mm from bottom of page to bottom of text (ignoring any footers)
  • margin-footer = distance in mm from bottom of page to bottom of footer

If you specify a header that extends further down the page than the margin-top, then the header and main text will overlap.

Alternative margin usage

The configuration variables $setAutoTopMargin and $setAutoBottomMargin can be set. Both default to false.

pad

If $setAutoTopMargin is set to 'pad' then the value for margin-top is used to set a fixed distance in mm (padding) between the bottom of the header and top of the main text. The converse is true with $setAutoBottomMargin.

stretch

If $setAutoTopMargin is set to 'stretch' then the margin-top sets a minimum distance in mm between the top of the page and the top of the main text, which expands if header is too large to fit.

The configuration variable $autoMarginPadding (default: 2, configurable) defines an additional distance in mm used as padding between the header and main text. This is the behaviour seen in Microsoft Word.

The converse is true with $setAutoBottomMargin.

Experimental!

Use of this function is probably best considered experimental. It is incompatible with some of the other functionality in mPDF, and has some limitations:

  • Using either pad or stretch, the values are set for the whole document - changes during the document will produce unpredictable results
  • Incompatible with orientation changes, $forceportraitheaders or $forceportraitmargins
  • Does not work with “write to current page” options for Headers
  • Incompatible with CSS @paged media
  • Cannot set a new Footer when already further down the page than there is room needed for footer
  • Potential error if using {nbpg} to define page total in an HTML header or footer. The top/bottom margin is calculated by writing the HTML header using the current page no. If when the header is finally written, a page number total of e.g. 500 causes an extra line in the header/footer, this will not be allowed for.
Fork me on GitHub