FPDI php useTemplate and formatting on every page

This was something I was searching long and hard for whilst writing a billing system I was working on.

I thought I’d share this here just in case anyone else finds it useful.

Using the excellent FPDI php template library to write php generated text onto a PDF template, I needed to have the generated text span over a few pages. I struggled to get the template to appear on each new page, and when I did I need to add a margin to the top of the document to prevent overwriting the PDF template header.

In the end the only way I could do it was by extending the FPDI class to run my own code in the header of all new pages

class MYPDF extends FPDI {
    var $_tplIdx;  				// Store the template id of the imported page

    function Header() { 			// Include a background template for every page
        if (is_null($this->_tplIdx)) {
            $this->_tplIdx = $this->importPage(1);
        $this->SetY(58);                        // Add a sufficent gap at the top of the page

    function Footer() {}