phpfpdffpdi

FPDI, FPDF SetAutoPageBreak add template to page after break


I've created a form that allows users to create a pdf that has an unlimited number of pages, I've got SetAutoPageBreak set so that it continues onto a second page however I cannot get the pages created after the page break to continue to use the original template file. The basic code can be seen below.

require('fpdf.php');
require('fpdi.php');

$pdf = new FPDI('P','mm','A4');

    $pageCount = $pdf->setSourceFile("source_file.pdf");
    $tplIdx = $pdf->importPage(1);
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx);
    $pdf->SetTextColor(63,76,89);
    $pdf->SetMargins(5,39,5,20);
    $pdf->SetAutoPageBreak(true,22); //page created doesn't have template attached
    $pdf->SetDrawColor(225,225,225);
    $pdf->SetFillColor(248,248,248);
    $pdf->SetLineWidth(1);
    $pdf->SetXY(82, 40);
    $pdf->MultiCell(165,5,$company.$block,0,L,false);
    $pdf->SetXY(19, 45);
    $pdf->MultiCell(165,5,$date.$block,0,L,false);
    $pdf->Output();

Having looked around, this question is the closest I can find however I'm not sure whether it is even relevant: FPDF/FPDI UseTemplate

Thanks


Solution

  • Just place the imported page in the Header method:

    class PDF extends FPDI
    {
        protected $_tplIdx;
    
        public function Header()
        {
            if (null === $this->_tplIdx) {
                $this->_tplIdx = $this->importPage(1);
            }
    
            $this->useTemplate($this->_tplIdx);
        }
    }
    
    $pdf = new PDF('P','mm','A4');
    $pdf->AddPage();
    ...
    

    ...and everything should work as expected.