FPDF TCPDF AJAX HTML Javascript jQuery PHP Example MORE

How to add multiple pages pdf in TCPDF using FPDI


/* Original file with multiple pages  */
$fullPathToFile = 'full/path/to/file.pdf';

class PDF extends FPDI {

    var $_tplIdx;

    function Header() {

        global $fullPathToFile;

        if (is_null($this->_tplIdx)) {

            /* THIS IS WHERE YOU GET THE NUMBER OF PAGES */
            $this->numPages = $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);

        }
        $this->useTemplate($this->_tplIdx);

    }

    function Footer() {}

}

/* initiate PDF */
$pdf = new PDF();
$pdf->setFontSubsetting(true);


/* add a page */
$pdf->AddPage();

/* The new content */
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');

/* THIS PUTS THE REMAINDER OF THE PAGES IN */
if($pdf->numPages>1) {
    for($i=2;$i<=$pdf->numPages;$i++) {
        $pdf->endPage();
        $pdf->_tplIdx = $pdf->importPage($i);
        $pdf->AddPage();
    }
}

/* Output the file as forced download */
$pdf->Output('theNewFile.pdf', 'D');

You get the number of pages by adding the first part of this line

$this->numPages = $this->setSourceFile($fullPathToFile);