Saltar al contenido
Contact : alejandrasalcedo0288@gmail.com

How to Design PDF Reports with FPDF and PHP

How to Design PDF Reports with FPDF and PHP Step by Step

When we talk about FPDF we are referring to a library, a kind of set of classes written in PHP that allows us as users to generate all types of PDF files, which in turn saves us a huge amount of work in PHP.

One of the keys to success that FPDF has among users is related to the fact that it is possible to configure many of the main parameters that are going to customize the document in question, such as the typeface or its size, its format, margins, etc.

How to Install the FPDF Bookcase on a Website

The first thing we have to consider when we talk about the possibility of installing FDPF is that we are talking about a library that represents a class, while for it to work we need to have minimal PHP 4. Therefore, if you don't have it, start by installing PHP.

Anyway, don't worry, since most of the servers already support PHP 5.3 or 5.4, and in case you don't have the library on your PC, you can download it easily from http://www.fpdf.org/.

Once you've downloaded it, you will see the ZIP file on the computer, which you have to unzip and color on your local server or on the final server, also creating a directory called PDF, in which you will upload the library files. Nothing else is needed for installation.

First Steps with FPDF from PHP

To start getting used to this system, we will show you below a simple example of how this library works, creating an index.php file, thanks to which you can place the code to display a PDF file with a title and text, like this:

<? php

require (‘pdf / fpdf.php’);

$ pdf = new FPDF (); $ pdf-> AddPage ();

$ pdf-> SetFont (‘Arial’, ’B’, 16); $ pdf-> Cell (50,10, ’Testing FPDF’, 1,1, ’L’);

$ pdf-> Cell (50.5, ’line break’, 1.1);

$ pdf-> SetFont (‘Arial’, ’B’, 10); $ pdf-> MultiCell (190.5, ’Lorem Ipsum or any filler text..’);

$ pdf-> Output ();

?> fpdf.jpg

From this, it is worth focusing on the Cell property, which has these parameters:

$ pdf-> Cell (50,10, ’Testing FPDF’, 1,1, ’L’);

Cell(width, height, text, border, line break, text alignment)

You have to take into account in these cases that the border can contemplate the values ​​1: with border and 0 without border. In the same way you can customize the line breaks so that they adapt specifically to what interests you.

In the same way, it is possible modify the code to remove the edges, converting the text to utf8 so that there are no problems with tildes or.

$ pdf-> Cell (50,10, ’Testing FPDF’, 0,1, ’L’);

$ pdf-> Cell (50.5, ”, 0.1);

$ pdf-> SetFont (‘Arial’, ’B’, 10);

// convert the text to utf8 $ text = utf8_decode (‘Lorem Ipsum…’);

$ pdf-> MultiCell (190.5, $ text);

When to use Cell or Multicell?

We know, on the other hand, that many users have doubts about when to use Cell or Multi Cell, and that is why in the following lines we want to show you the differences between them, so that you take them into account:

  • Cell: used to create a cell, although we have to know exactly the length of the content so that it is not deformed.
  • Super Cell: useful when we have paragraphs that we do not know how much they occupy, since the cells will adapt to the content.

In any case, the important thing is that we will be able to use Cell or Super Cell when we consider it appropriate, so you only have to define what type of font you want to use in these cells before invoking the function.

Again, so you don't have any doubts, we will show you next what are the steps you have to follow when you want to create a header or footer for your PDF, using the FPDF Header () and Footer () methods.

The advantage of these methods is that they run almost automatically So the effort that we have to make as users is minimal, and as long as it can be saved when we don't have great computer skills, it is appreciated.

An example thinking about sale of products With its prices, it will be the following:

<? php

require (‘pdf / fpdf.php’);

// We create the new pdf class that inherits from fpdf

class PDF extends FPDF {

// use the Header () function and customize it to show the header of pginafunction Header ()

{

// set the font Arial Bold 16 $ this-> SetFont (‘Arial’, ’B’, 16);

// we put a cell without content to center the title or the cell of the title to the right $ this-> Cell (50);

// define the cell the title $ this-> Cell (100,10, ’Sales Report Header’, 1,0, ’C’);

// Line break jumps 20 lines $ this-> Ln (20);

}

// we use the Footer () function and customize it to show the footer of pginafunction Footer ()

{// Set the position of the next cell fixedly 1.5 cm from the bottom of the page

$ this-> SetY (-15); // Set the Arial italica 10 font

$ this-> SetFont (‘Arial’, ’I’, 10); // Page number

$ this-> Cell (0.10, ’Page‘. $ this-> PageNo (). ’/ {nb}’, 0.0, ’C’);}

} $ this-> PageNo (We will set the page number)

Once we have considered the above, the next step has to do with creating a list of products in PDF format, so that we will take them from a database in MySQL and thanks to PHP we will generate the list automatically.

In this example we are not going to add auxiliary tables of categories and items:

—— Database: `market`

—— Table structure for the `products` table

CREATE TABLE IF NOT EXISTS `products` (` id_publicacion` int (11) NOT NULL AUTO_INCREMENT,

`idrubro` int (11) DEFAULT NULL,` idcategory` int (11) DEFAULT NULL,

`idsubcategory` int (11) DEFAULT NULL,` image1` varchar (255) DEFAULT NULL,

`image2` varchar (255) DEFAULT NULL,` image3` varchar (255) DEFAULT NULL,

`image4` varchar (255) DEFAULT NULL,` image5` varchar (255) DEFAULT NULL,

`image6` varchar (255) DEFAULT NULL,` price` decimal (10,2) DEFAULT NULL,

`active` varchar (3) DEFAULT NULL,` comment` text,

`title` varchar (255) DEFAULT NULL,` stock` int (11) DEFAULT NULL,

PRIMARY KEY (`post_id`)) ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;

Considering the above, the only thing we need is to connect through PHP to the database to check all products in this way:

<? php error_reporting (0);

require (‘pdf / fpdf.php’);

// I connect to the database $ link = mysql_connect ("localhost", "root");

mysql_select_db (“market”, $ link); // Check the products table requesting all products

$ result = mysql_query ("SELECT * FROM products", $ link);

// We installed the class to generate the document pdf $ pdf = new FPDF ();

// We add the first page to the document pdf $ pdf-> AddPage ();

// Set the start of the upper margin in 25 pixels

$ y_axis_initial = 25;

// Set the typeface and create the title of the page. Not a header will not be repeated $ pdf-> SetFont (‘Arial’, ’B’, 12);

$ pdf-> Cell (40.6, ”, 0.0,’ C ’); $ pdf-> Cell (100.6,’ PRODUCT LIST ’, 1.0,’ C ’);

$ pdf-> Ln (10);

// We create the cells for the titles of each column and assign a gray background and the font $ pdf-> SetFillColor (232,232,232);

$ pdf-> SetFont (‘Arial’, ’B’, 10); $ pdf-> Cell (125.6, ’Title’, 1.0, ’C’, 1);

$ pdf-> Cell (30.6, ’Price’, 1.0, ’C’, 1); $ pdf-> Cell (30.6, ’Photo’, 1.0, ’C’, 1);

$ pdf-> Ln (10);

// I start creating the product lists according to the MySQL query

while ($ row = mysql_fetch_array ($ result)) {

$ title = $ row (‘title’);

$ price = $ row (‘price’); $ image = ”photos /†.$ row (‘ image1 ′);

$ pdf-> Cell (125.15, $ title, 1.0, ’L’, 0);

$ pdf-> Cell (30.15, $ price, 1.0, ’R’, 1); // Show the image inside the GetX cell and GetY give the current coordinates of the row

$ pdf-> Cell (30, 15, $ pdf-> Image ($ image, $ pdf-> GetX () + 5, $ pdf-> GetY () + 3, 20), 1, 0, 'C', false);

$ pdf-> Ln (15);

}

mysql_close ($ link);

// We show the document pdf $ pdf-> Output ();

?>.

Have you been able to create your first PDF reports with FPDF and PHP thanks to this trick?

Rate this post