PaulMrG PaulMrG - 2 months ago 61
PHP Question

TCPDF and FPDI with multiple pages

This looks like the simplest thing but I can't get it to work.

I need to add text to the first page of a multi-page pdf (could be any number of pages)

Using this code on a two page pdf (without the for loop, just using $pdf->importPage(2)) I end up with two pages but the second page is a repeat of page one. The text is written on the first page only which is good but I need all pages included in the output pdf. Here is my code

// 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->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');

// How to get the number of pages of original pdf???
// $numPages = $pdf->getNumPages(???);

// Carry on adding all remaining pages starting from page 2
for($i=2;$i<=$numPages;$i++) {
// Add another page
$pdf->AddPage();
// Do I need to declare the source file here?
// $pdf->setSourceFile($fullPathToWD);
$pdf->importPage($i);
}

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


Links to docs

TCPDF Classes
http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced

FPDI Classes
http://www.setasign.de/support/manuals/fpdi/

FPDF_TPL Classes
http://www.setasign.de/support/manuals/fpdf-tpl/

Answer

Solved my problem...

// 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);

And see the second last block of code - the for loop adds the remainder of the pages.

Don't know if this is how it should be done? I read in a few places that it wasn't even possible to achieve this, also the code is not supplied in the docs. However, this works, hope it helps someone.