United States|United Kingdom|India
sudeepg
May 08, 2010

In PHP, we have a nice open source library for PDF file generation, FPDF. This library has an extention FPDI that allows for manipulating PDF files. FPDI extracts the content of the pdf, allows you to change it and then output the changed pdf.

 

 

Example Usage: FPDF. Creating First PDF file.

 

 

Download and unzip FPDF package and include it in your php script. The following code gives an example on how to create your first pdf file named myfile.pdf:

<?php
require(
'fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output('myFile.pdf', 'F');
?>

The above code creates a downloadable pdf file (myFile.pdf) from the php code with "Hello World" in it. Here, we are first creating a PDF object, then adding a page. In the new page, we then set the font style to Arial with font size 16. Next, we create  a cell with 40px wide and 10px height that contains the text "Hello World".

 

For more examples on pdf manipulation with your php code, checkout the link.