Installation and first steps to generate PDF in PHP with FPDF

FPDF is a library for the dynamic generation of PDF documents in PHP. We start the FPDF manual with the installation and a first example of use.

FPDF is a class developed in PHP to be able to create PDF documents dynamically from our PHP scripts. This class works completely autonomously, so it does not require the use of the PDFlib library or any other similar product.

It is a class that gives a lot of play, since we can modify the unit of measurement, the page format, the margins, the headers and footers, the line breaks, the images, colors, links, etc.

An important detail is that FPDF is a free library for any use, both commercial and personal (its first initial “F” means just “Free”, that is, free and free), so it will be very interesting for any use. In this .com article we are going to focus on an explanation of its installation and then we will see a first example of use.

FPDF Installation

This class requires at least PHP 4 or higher. It also has some extensions that can be useful.

After this brief introduction, we are going to download and install the necessary libraries for its use. To do this, we downloaded the latest version of FPDF in the following, in the download section.

Once downloaded, we upload it to our server and place it in a folder called fpdf in the root of the domain, or any other directory we want, as long as we remember where we have put it and include it correctly in the scripts where we plan to use it.

See also  How to measure the performance of a computer

The installation does not require any other requirements, so we will verify that it is quite simple.

Working with FPDF to generate a PDF from PHP

From here we can begin to see how to program with FPDF. It really is used like any object-oriented programming class with PHP, so the most important thing will be to know and master its different methods and properties. Let’s start with a very simple example that doesn’t require a lot of PHP programming.

The general format to write a page in PDF would be the following:

AddPage(); $pdf->SetFont(‘Arial’,’B’,16); $pdf->Cell(40,10,’My first pdf page with FPDF!’); $pdf->Output(); ?>

If we execute this directly, a PDF file will appear with the title that we have put.

Let’s analyze all the written lines:

  • The first thing we do is include the fpdf.php library
  • In the line $pdf=new FPDF(); what we are doing is creating the FPDF object. If we do not put anything between the parentheses, the object will be created with the default values, in this case they would be the following: the page size is A4, the elongated format and the unit of measurement is millimeter. If we want to modify these parameters, it would be in the following order: $pdf=new FPDF(‘format’,’unit of measure’,’size’);
  • In the line $pfd->AddPage(); we add a page
  • With SetFont(); we format the text by saying the type of font, if it is bold or not, and the size of the font.
  • Already in the line $pdf->Cell(); We start writing the content of the page. We start by saying the width of the cell where we are going to write, the height of the cell, and the content of the cell. It has some more parameters that we will explain in detail in the following articles.
  • The last line $pdf->Output(); what it does is close the file and send it to the browser. It is important not to put this line before finishing writing the file as it will give us an error. Also, if you don’t put it right at the end and write a few more lines of code not related to the PDF, the document may appear blank.
See also  How is a website inside

Well, as we can see, this first example is simple, although we have not delved into all the options of the functions that we have seen for the generation of PFD files from PHP.

In the next article we will look at each one of those named above and then we will start to give the header a bit of style.

In the rest of the one that follows you will also learn many other interesting things, such as the creation of tables, insertion of images, texts coming from external files and more.

Loading Facebook Comments ...
Loading Disqus Comments ...