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.

<?php
require(‘fpdf.php’);
?>

Now we can create a hello world pdf with FPDF

<?php

require(‘fpdf.php’);

$pdf = new FPDF();
$pdf->AddPage();  // Add A4 paper by default
$pdf->SetFont('Arial','B',16);  // Add fonts style to your pdf
$pdf->Cell(40,10,'Hello World!');  //  Add single line content to your PDF
$pdf->Output();// Generate PDF

?>

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:

Cell(float w [, float h [, string txt [, mixed border  [, int ln [, string align [, int fill [, mixed link]]]]]]])

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:

Y-axis :  $pdf->SetY(Y-axis no eg{ 10, 30 });

X-axis :  $pdf->SetX(X-axis no eg{ 10, 30 });

Complete Code:

$pdf->SetY(223);

$pdf->SetTextColor(0,171,89);

$pdf->SetFont('Times','BI',18);

$pdf->Cell(0,5,'Board of Director', 0,1,'L');

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.

$pdf->Cell(0,5,'Board of Director', 0,1,'L');

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

MultiCell();

Complete code:

$pdf->SetY(110);

$pdf->SetTextColor(0,0,0);

$pdf->SetFont('Times','I',19);

$pdf->MultiCell(0,14,' my paragraph ');

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 :

$pdf->Image(“imagesource.jpg”,’X-position’, ’Y-position’, ‘Image width’, ‘Image height’', “image hyperlink”');

Code

$pdf->Image($imgSvp,12,48,17,30,'');

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

$pdf->SetY(223);$pdf->SetTextColor(0,171,89);$pdf->SetFont('Times','BI',18);

$pdf->Cell(0,5,'Cool', 0,1,'L');

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

$pdf->image('bg.jpg');

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

$pdf->Output();

Sample Code:

<?php

require_once('fpdf17/fpdf.php');

$pdf= new FPDF(); // create object of FPDF class

$pdf->AddPage();

$pdf->SetDisplayMode('real','default');

$pdf->SetFont('Arial','',18);

$pdf->SetXY(0,0);

$pdf->SetY(7);

$pdf->SetTextColor(0,0,0);

$pdf->Cell(0,5, 'Hello world', 0,1,'L');

 

// First we will change the XY position to the bottom left hand corner of the page to display our page number in.

$pdf->SetXY(160, 272);

// Now we display our page number using the Cell function.

$pdf->Cell(30, 4, 'Page ' . $pdf->PageNo(), 0, 1);

$pdf->Output();

?>