Manipulating PDF files with PHP using FPDF

In .NET, we have certain libraries that do pdf manipulation for us.

In PHP, we have open source library for PDF file generation called FPDF. There is an extension of the library: FPDI that allows for manipulating PDF files.

FPDI extracts the content of the pdf as a template, allows us to change the content and then output the changed pdf as a new pdf file.

There is no install. We need to download and include fdpf.php and fdpi.php libraries in our php script as would be demonstrated in the examples below:

Example 1: Creating your first PDF from the php code


$pdf->Cell(40,10,’Hello World!’);

Example 2: Changing the content of the PDF


$pdf =& new FPDI();

//Set the source PDF file
$pagecount = $pdf->setSourceFile(“existing_pdf.pdf”);

//Import the first page of the file
$tpl = $pdf->importPage(1);

//Use this page as template
// use the imported page and place it at point 20,30 with a width of 170 mm
$pdf->useTemplate($template, 20, 30, 170);

#Print Hello World at the bottom of the page

//Select Arial italic 8
$pdf->SetXY(90, 160);

$pdf->Write(0, “Hello World”);

$pdf->Output(“modified_pdf.pdf”, “F”);

The above code in example 2 takes a PDF file “existing_pdf.pdf”, and creates a copy of it “modified_pdf.pdf” with “Hello World” printed.

Comments on this entry are closed.