fpdf import pdf file

You are using fpdf to generate PDF file in php. You want to use an existing PDF file to import header and footer ? This is easy :

First, download FPDI lib here

and do that on your code :

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

$pdf = new FPDI();   // FPDI and not FPDF
$pdf->AliasNbPages();
$pdf->AddPage();
$pagecount = $pdf->setSourceFile(SF_ROOT_DIR.'/lib/fpdf/courrier.pdf');
$tplix = $pdf->importPage(1);
$pdf->useTemplate($tplix);
$pdf->Output('newdocument.pdf', 'D'); 

If you get this error

Warning: require_once(fpdf_tpl.php): failed to open stream:

you need to get the FPDF_TPL class too.

This only import file for the first page, if you want it on every page, override AddPage() function :

public function AddPage($orientation='', $size='', $keepmargins=false, $tocpage=false)
{
    parent::AddPage();
    $pagecount = $this->setSourceFile(SF_ROOT_DIR.'/lib/fpdf/courrier.pdf');                                                          
    $tplix = $this->importPage(1);
    $this->useTemplate($tplix);
}

Leave a Reply