If your are creating an online application which we need to generate a PDF online. Then this tut is for you we are going to create an application in which we generate a PDF online. For creating a PDF dynamically we are using a FPDF library.

FPDF?

FPDF is an alternative way to generate PDF files with PHP. A free class containing a number of functions for creating and manipulating PDFs.

FPDF has some advantages than other online pdf generator.

  1. Choice of measure unit, page format and margins
  2. Page header and footer management
  3. Automatic page break
  4. Automatic line break and text justification
  5. Image support (JPEG, PNG and GIF)
  6. Colors
  7. Links
  8. TrueType, Type1 and encoding support
  9. Page compression

for more details and information http://www.fpdf.org/.

If you want to download please follow this link: http://www.fpdf.org/en/download.php.

FPDF does not need any installation to server you simply download this this library and include this to your page using php include() / require().

Start creating PDF file

Before start, you need to download the FPDF class from the FPDF web site i already give a link above to download.
After download add FPHP class to your web page.

Now we can create a hello world pdf with FPDF

In above code we simply call some default function of FPDF to generate pdf.

There is a method in FPDF called the document’s cell. This refers to a rectangular area on the page that you can create and control. The cell can have a height, width, border, and, of course, text. The basic syntax for the cell method is as follows:

If you want to adjust the  location of your text use:

  • L – Left – like : $pdf->Cell(0,5, ‘Hello world’, 0,1,’L’);
  • R – Right – like : $pdf->Cell(0,5, ‘Hello world’, 0,1,’R’);
  • C – Center – like : $pdf->Cell(0,5, ‘Hello world’, 0,1,’C’);

If you want to adjust the location of the text in X-axis or Y- axis you use this:

Complete Code:

One thing you keep in your mind that you have to declare your X-axis or Y-axis before creating your content shown in above code.

Cell() function work very well for small content but if you have large content or paragraph then you have to use

Complete code:

Image in your PDF

Using FPDF we also add image in our pdf file and also add link to image. You also adjust your image position over PDF in any where.

Complete Code

 Syntax :

Code

Now if you want to make your font look Italic or Bold or both then you use this

  • BIU

  • BI

  • U

  • B

  • I

  • UB

Or any combination of above.

Complete Code

You also define your color for your text by using SetTextColor(R, G, B) as shown in above example.

You also add image background for your PDF by simply using this code

Now finally at end you have to use this code so your PDF is generated

Sample Code: