How to Edit Existing pdf using fpdf Library in PHP

By the help of FPDI library, I tried to write some exsiting pdf files but able to wirte only single page of pdf.
Below is my code:

[PHP]require_once ‘…/includes/fpdf/fpdf.php’;
require_once ‘…/includes/fpdf/fpdi.php’;
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile(“contractFinalTrans.pdf”);
$tplIdx = $pdf->importPage(1);
$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 200);
//set position in pdf document
// now write some text above the imported page
//$pdf->SetFont(‘Arial’);
$pdf->SetFontSize(10);
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(50, 50);
$pdf->Write(0, “page 1”);///print this output
$pdf->SetAutoPageBreak(true,22);
$pdf->addPage();
$tplIdx = $pdf->importPage(2);
//$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 200);
//set position in pdf document
// now write some text above the imported page
//$pdf->SetFont(‘Arial’);
$pdf->SetFontSize(10);
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(100 , 100);
$pdf->Write(0, “page 2”);
$pdf->Output(); [/PHP]

Can anyone suggest me how to Edit Existing multiple pdf using fpdf Library in PHP

I have explored many php developer forum related to this issue but I am unable to get satisfactory solutions. Hope this forum help me out for the same issue.

If you mean you wish to load multiple PDF files, you would have to create a loop to read in all of them
one after another and then process the info.

Your current code show creation of two pages, but, it does not loop thru the pages of the original.
You get the page count into variable $pageCount ! Therefore, just loop thru the pages importing each.

In general something like …
$pageCount = $pdf->setSourceFile(“contractFinalTrans.pdf”);
for( $i= 0; $i <= $pageCount; $i++ ) {
$tplIdx = $pdf->importPage($i);
… etc …
}

In other words, you have the number of pages in the input PDF, but, you don’t loop thru them, you just
pull the first page and then the second page in code. Only one routine is needed for pulling the pages, but,
you must loop thru all of the pages…

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service