Dynamic PDF generation using PHP

PDF format is very common document format in present IT world. As same as its popularity, Dynamic PDF generation is very important requirement for some project because PDF very famous document format for creating reports and some document and sharing.

Based on my ongoing PHP project, it has one requirement to generate PDF dynamically, based on predefined PDF template. Whilst searching, I could find a solution to generating PDF on the fly.

I used two libraries called FPDF and FPDI. Then, I wrote php code to generate document based on template on the fly.

Here, it loads page by page and insert the required text, which is to be added dynamically. Following is the example.


<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');


// initiate FPDI
$pdf = new FPDI();


// add a page
$pdf->AddPage();

// set the sourcefile
$pdf->setSourceFile('Sample.pdf');


// import page 1
$tplIdx = $pdf->importPage(1);


// use the imported page and place it at point 0, 0 with a width of 210 mm and height of 297mm
$pdf->useTemplate($tplIdx, 0, 0, 210, 297);


// now write some text above the imported page
$pdf->SetFont('Arial');
$pdf->SetFontSize(10);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(35.7, 116.5);
$pdf->Write(0, "first_name last name");
$pdf->SetXY(35.7, 120.9);
$pdf->Write(0, "street 1 2");

// import page 2
$tplIdx = $pdf->importPage(2);


// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 0, 0, 210, 297);


// now write some text above the imported page
$pdf->SetFont('Times', ‘B’, ‘24’);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(35.7, 116.5);
$pdf->Write(0, "first_name, last name");

$pdf->Output('NewFile.pdf', 'I');

?>

Generated output file can be retrieved using either from 4 options as follows.
e.g.: $pdf->Output($filename, $option);
This sends the file to either given location to be saved, browser or string. When it sends to browser, user can download it as file or open it inside browser using pdf reader plug-in installed.

Paramerts :

$filename :
The name of the file. If not specified, the document will be sent to the browser (destination I) with the name doc.pdf.

$option :
Destination where to send the document. It can take one of the following values:

  • I: send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the “Save as” option on the link generating the PDF.
  • D: send to the browser and force a file download with the name given by name.
  • F: save to a local file with the name given by name (may include a path).
  • S: return the document as a string. name is ignored.

References :
http://www.setasign.de
http://www.fpdf.org/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s