Skip to content
Advertisement

How to add a external pdf file as an additional page to TCPF via FPDI

I’m using TCPDF to create PDF files from php files. Works all fine. Now I want to add an additional page using an existing .pdf file from the server.

Best method to do so is using FPDI afaik.

But I can’t find any Docu or working example on how to setup FPDI within TCPDF to add a Page. All I see is how I use an external pdf as header or background etc.

like this one https://www.setasign.com/products/fpdi/about/

What I have in TCPDF:

use setasignFpdiTcpdfFpdi;

// require_once('tcpdf/config/lang/eng.php');
require_once('TCPDF-main/tcpdf.php');
require_once('FPDI/src/autoload.php');

// Extend the TCPDF class to create custom Footer
class MYPDF extends TCPDF {

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

// add external PDF with FPDI (not working)

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);

$pageCount = $pdf->setSourceFile("hiking.pdf");
$tplIdx = $pdf->importPage(1, '/flyer');

$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 90);

$pdf->Output();


// create new PDF document with TCPDF (working)

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(true);

$pdf->AddPage();
ob_start(); //Start new output buffer

//Write page 1
$html = include('mytour.php');

$html = ob_get_contents();

$pdf->writeHTML($html, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();
ob_end_clean();

// ---------------------------------------------------------

$cdate = date("y-d-m");
$ctime = date("H-i-s");

//Close and output PDF document
$output1 = "mytour.pdf";
$pdf->Output($output1, 'I');

Error I receive: PHP Fatal error: Uncaught Error: Call to undefined method MYPDF::setSourceFile()

Appreciate any hint on how to do it.

Advertisement

Answer

Just extend the correct class as documented here:

class MYPDF extends setasignFpdiTcpdfFpdi {
...
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement