Easy way to generate PDF in PHP

9 Oct

Working with PDF is really sticky. We need to calculate every pixel. And if we use ZEND LIBRARY arranging the layout is also a big issue. Recently I used FPDF (http://www.fpdf.org/) library it is really nice thing. [Thnx to HASIN BHAI  as he referd me to FPDF.]

Using  FPDF is really easy and fancy. For layout it use cells almost like table. Sot we can add text any where easily.

They have a good tutorial to learn it in a second. I am just describing their fired example

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

In first line we how to include the PDF library.

$pdf=new FPDF();
This is the reference of the fpdf library.

This instruction use to arrange the page layout. We can send some string parameter to define the page like ‘A4’ ‘L’ (Landscape) etc.

This line is use to define the font. But have to keep in mind that we need the font folder with font reference with script.

There are some  extra instruction like
$this->SetDrawColor(0,80,180); // Define the border color or graphics color
$this->SetFillColor(230,230,0);  //  The graphics or cell background color
$this->SetTextColor(220,50,50); // Text color.

To add image we can use a simple function  

Now to add text normally we use cell. This is a very commonly used function in FPDF with some parameter.
First tow parameter means width and height. Third parameter is txt.
Fourth parameter define border. If we put 1 will enable border in all side. Also can send T for top, B for bottom, L for left and R for right. Let’s say we want to put border at top and left side then we have to write like this $pdf->Cell(20,10,’Title’,’LT’,1,’C’);
Fifth parameter defines the alignment. 0 for arrange after your current cell and 1 for put in new line.
Sixth parameter defines the text alignment.
Seventh parameter user for cell background must be 1 for colored or 0 for transparent. Default value: 0.

Now to send the PDF in output we user $pdf->output() function. If we want to  show in browser instantly just simply use  $pdf->Output(); and if we want to save as file then just enter the path in the function like $pdf->Output(‘/home/pub_html/text.pdf’);

Another on instruction we use frequently is $pdf->Ln() to go next line by force.


3 Responses to “Easy way to generate PDF in PHP”

  1. papuas November 2, 2007 at 9:51 am #

    hmmm howto torate text with text formatting in multiple cells documents [tables]
    i need this to create print-forms in my job 😉 greate think, but really sall pleace to represent output… with rotate text is more usability 😉


  2. manchumahara December 4, 2007 at 8:55 pm #

    Thank you very much for this article. I was finding something like this..

    Sabuj aka manchumahara

  3. Gaurav May 28, 2008 at 10:51 am #


    I am using fpdf in cakephp to generate pdf document on the fly but it is successfully generating pdf document in mozilla but not in IE.
    Please suggest for the same.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: