Tutorial :Edit PDF files programmatically in PHP


Background info: I got a project to produce a customised PDF on the fly from a given PDF file using PHP. All I need it to do is to replace strings, e.g. search in "template.pdf" for "{Address}", replace with "Street Name".

I've seen links to fpdf/pdfi/dompdf etc., but can't find any useful example code that I could use :s. Any help / pointers would be greatly appreciated.


fpdf is fantastic, you need to use somthing else to import an exisitng PDF though, See below.


require_once('fpdf.php');  require_once('fpdi.php');    $pdf =& new FPDI();    $pagecount = $pdf->setSourceFile('TestDoc.pdf');  $tplidx = $pdf->importPage(1, '/MediaBox');    $pdf->addPage();  $pdf->useTemplate($tplidx, 10, 10, 90);    $pdf->Output('newpdf.pdf', 'D');  


decided to generate html web page (PHP) then use wkhtmltopdf (http://code.google.com/p/wkhtmltopdf)

to produce the pdf bit of a work around but less hastle


PDFlib (with the additional PDI) from pdflib.com should be able to do this for you. Admittedly it is pretty pricey, so there may be other options, too :)


You may use FPDF or TCPDF Library classes to do so. With these classes, you can quickly, easily and effectively generate/edit PDF files.

Have a look at this article which informs about these two libraries: Easily create PDF files on the fly with PHP

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »