šŸ“œ ā¬†ļø ā¬‡ļø

Generating PDF files with PHP and FPDF

Most web services export data in different formats for future use. This article is about how to export data in pdf-format.
Although many know how to do this, I will describe briefly for those who do not know.

PHP allows us to generate pdf files on the fly. FPDF is a free php code that allows you to create documents in pdf format and perform various manipulations with them.

PDFlib
PHP API contains a large number of functions for working with PDF, implemented on the basis of PDFlib . Despite this, this library is not free for commercial use. The free version is called PDFlib Lite and is free for personal use, however it is limited in functionality. In order to use the full PDFlib library you need to purchase a license.

Why FPDF?
The alternative is to use FPDF, a free class containing a large number of functions for creating and manipulating PDF documents. The key word for this moment is its free. You can download, use and modify this class as you like. In addition to being free, this library is much simpler than PDFlib. To use PDFlib you need to install it as an extension to PHP, while FPDF can be directly connected to the program.
')
Creating PDF Documents
To get started, you need to download the FPDF code from the FPDF Web site and include it in the program. For example, like this
require ('fpdf.php');

Below is an example of using a library to generate a simple PDF.
We will create a new FPDF object:
$ pdf = new FPDF ();

The FPDF constructor takes the following parameters.
Next we set some properties of the document.
$ pdf-> SetAuthor ('Lana Kovacevic');
$ pdf-> SetTitle ('FPDF tutorial');

Since in this example we use the same font for the whole document, we set it before creating the page
$ pdf-> SetFont ('Helvetica', 'B', 20);
$ pdf-> SetTextColor (50,60,100);

The SetFont function has 3 parameters; font name, style and size. We use Helvetica, bold and 20 items, we will use it for the title of the document.
You can use any other font using the AddFont function.
Using the SetTextColor function, we set the font color for the entire document. Color can be represented in RGB or gray scale. In this example, we use RGB values.
Now that the main thing is done, let's start creating pages.
$ pdf-> AddPage ('P');
$ pdf-> SetDisplayMode ('real', 'default');

You can pass ā€œPā€ or ā€œLā€ parameters to the AddPage () function to specify the page orientation. The SetDisplayMode function determines how the page will be displayed. You can define zoom and markup options. In the example, we use the 100% magnification and default markup defined in the program used for viewing.

Now that we have a page, let's insert an image into it in order to make the page more pleasant, and we will also add a link. We will display the FPDF logo using the Image function and pass the following parameters to it - the file name, the dimension and the address.
$ pdf-> Image ('logo.png', 10,20,33,0, '', 'http: //www.fpdf.org/');

To add a link, use the following command.
$ pdf-> Link (10, 20, 33.33, 'http://www.fpdf.org/');

Now create a header with a frame.
$ pdf-> SetXY (50,20);
$ pdf-> SetDrawColor (50,60,100);
$ pdf-> Cell (100.10, 'FPDF Tutorial', 1.0, 'C', 0);

The SetXY function sets the x and y coordinates of the point at which we want to display the caption. SetDrawColor sets the border color using RGB values. After that we call the Cell function to display a rectangle with the text of our title. We pass the following parameters to the function: width, height, text, border, ln, alignment and padding. Bound value 0 - no border or 1 for the presence of a border. For ln, we use the default value 0, ā€œCā€ - alignment of the text on the center and 0 for the fill parameter. If we set the last parameter to 1, our rectangle would be filled in, a value of 0 will leave it transparent.
Now we want to write a little text in our document.
$ pdf-> SetXY (10.50);
$ pdf-> SetFontSize (10);
$ pdf-> Write (5, 'Congratulations! You have generated a PDF.');

So again, we set the output coordinates of the text x and y, but now we will reduce the font size using SetFontSize. The Write function will print the text in our document. Parameter 5 sets the height; it only makes sense when we have many lines in our text.
In the end, we output our result using the Output function.
$ pdf-> Output ('example1.pdf', 'I');

Here we specified the file name and output parameters, in this case ā€œIā€. The ā€œIā€ parameter will display the result in a browser.

So the full text:
require ('fpdf.php');
// create a FPDF object
$ pdf = new FPDF ();
// set document properties
$ pdf-> SetAuthor ('Lana Kovacevic');
$ pdf-> SetTitle ('FPDF tutorial');
// set font for the entire document
$ pdf-> SetFont ('Helvetica', 'B', 20);
$ pdf-> SetTextColor (50,60,100);
// set up a page
$ pdf-> AddPage ('P');
$ pdf-> SetDisplayMode (real, 'default');
// insert an image and make it a link
$ pdf-> Image ('logo.png', 10,20,33,0, '', 'http: //www.fpdf.org/');
// display the title with a border around it
$ pdf-> SetXY (50,20);
$ pdf-> SetDrawColor (50,60,100);
$ pdf-> Cell (100.10, 'FPDF Tutorial', 1.0, 'C', 0);
// Set x and y
$ pdf-> SetXY (10.50);
$ pdf-> SetFontSize (10);
$ pdf-> Write (5, 'Congratulations! You have generated a PDF.');
// Output the document
$ pdf-> Output ('example1.pdf', 'I');


Now that we have learned how to create documents, let's see what else can be done using FPDF. The example below shows us how to create the top and bottom (header and footer :-)) of our document.
require ('fpdf.php');
class PDF extends FPDF
{
function header ()
{
$ this-> Image ('logo.png', 10,8,33);
$ this-> SetFont ('Helvetica', 'B', 15);
$ this-> SetXY (50, 10);
$ this-> Cell (0.10, 'This is a header', 1.0, 'C');
}
function footer ()
{
$ this-> SetXY (100, -15);
$ this-> SetFont ('Helvetica', 'I', 10);
$ this-> Write (5, 'This is a footer');
}
}
$ pdf = new PDF ();
$ pdf-> AddPage ();
$ pdf-> Output ('example2.pdf', 'D');

As you can see, we created a child class using the inheritance and creation of Header and Footer functions. Then we created a new object and added a page to the document. The AddPage function will automatically call the Header and Footer functions. At the end, we output the received information to a file called example2.pdf, using the value ā€œDā€. In this case, the browser will offer to save the file.

So, we learned the basics of creating PDF documents, for more details use the FPDF Web site .

Russian version of the documentation ZIP and TGZ

Source: https://habr.com/ru/post/31438/


All Articles