+4 votes
746 views
Create PDF reports from PHP and the FPDF library

in PHP by (551k points)
reopened | 746 views

1 Answer

+5 votes
Best answer

We started by downloading the library from www.fpdf.org , once downloaded we unzipped and copied the directory fpdf into the directory of our application.

A simple example:

 <? php require ('fpdf17 / fpdf.php'); $ pdf = new FPDF (); $ pdf-> AddPage (); $ pdf-> SetFont ('Arial', 'B', 16); $ pdf-> Cell (80, 10, 'This is a cell of 40 x 10', 1); $ pdf-> Cell (50, 10, 'Cell of 50 x 10', 1); $ pdf-> Ln (10); $ pdf-> SetFont ('Arial', 'I', 12); $ pdf-> Cell (80, 10, 'This is a cell of 40 x 10', 0); $ pdf-> Cell (50, 10, 'Cell of 50 x 10', 0); $ pdf-> Output (); ?> 

We call the library from the fpdf directory, create an object and add a page with AddPage (), we set the type of letters style and size. Then we create a cell to contain data with the following structure

 $ pdf-> Cell (width, height, content, edge, alignment); // Alignment L Left, C Centering, R Right 

image


The result of the previous code in pdf will be:
The function Output is the one that shows the pdf if we put the parameter I will send the file to the browser, if we put the parameter D show the download window, therefore if in the previous code we write

 $ pdf-> Output ('mipdf.pdf', 'd'); 

As a result, the download window will appear to save the pdf.

We can also make more complex documents such as invoices, budgets or commercial reports, use databases like mysql.

In the following example we will see how to create a budget with a header image


image


 <? php require ('fpdf16 / fpdf.php'); $ pdf = new FPDF (); $ pdf-> AddPage (); $ pdf-> SetFont ('Arial', 'B', 16); // insert the header by putting an image inside a cell $ pdf-> Cell (700.85, $ pdf-> Image ('./ images / logo-factura.jpg', 30,12,160), 0,0, 'C'); $ pdf-> Cell (100.12, "Budget:". $ campodb ['nropresuputao']); $ pdf-> Cell (100.12, "Date:". date ('d / m / Y')); $ pdf-> Line (35,40,190,40); $ pdf-> Ln (7); $ pdf-> Cell (100.12, "Name:". $ campodb ['name'])); $ pdf-> Cell (90.12, "Nif:". $ rowcli ['nif']); $ pdf-> Line (35,48,190,48); $ pdf-> Ln (7); $ pdf-> Cell (100.12, "Address:". $ campodb ['address']); $ pdf-> Line (35,56,190.56); $ pdf-> Ln (7); $ pdf-> Cell (90.12, accents ("Phone:". $ campodb ['telefono'])); $ pdf-> Line (35,62,190.62); $ pdf-> Ln (7); $ pdf-> Cell (100.12, "Team:". $ campodb ['computer']); $ pdf-> Line (35,68,190,68); $ pdf-> Ln (9); $ pdf-> SetFont ('Arial', 'B', 10); $ pdf-> Cell (60.12, 'BUDGET'); $ pdf-> Ln (2); $ pdf-> SetFont ('Arial', '', 8); ?> 

Then by means of a query to the database plus a cycle while I generate the other cells and calculate the amounts.

 <? while ($ i <count ($ conultadb)) { [indent = 1] $ pdf-> Ln (7); [/ indent] [indent = 1] $ pdf-> Cell (139,7, accents ($ campodb ['concept']), 1,0, '1'); [/ indent] [indent = 1] $ pdf-> Cell (15,7, chr (128). "" .number_format ($ campodb ['price'], 2, '.', ''), 1,0, 'R' ); [/ indent] [indent = 1] // I calculate the total [/ indent] $ price = $ price + $ fielddb ['price']; }?> 

Then we create the remaining cells for the totals, VAT, and taxes as appropriate

 <? $ pdf-> Ln (7); $ pdf-> Cell (110.7); $ pdf-> Cell (29.8, "TOTAL", 1.0, 'R'); $ pdf-> Cell (15,8, chr (128). "" .number_format ($ priceconiva, 2, '.', ''), 1,0, 'R'); $ pdf-> Ln (20); $ pdf-> Multicell (400.4, "Note:". $ rowrepar ['notapresu']); ?> 

We also add a Multicell, which fulfills the function of several cells in one serves for large blocks of text as a footnote

Finally we give a name to our file, to which we can put an id that lists them according to a database and we define the option to download.

 <? $ file = 'budget-00.pdf'; $ pdfdoc = $ pdf-> Output ($ file, "D"); ?> 

Another interesting application is to combine fpdf with classes to generate bar codes with I25, EAN 39, 128, etc. On the fpdf website there are some examples and other script contributed by the community to test. Here is an example of a payment stub that customers download from a website.

image


A very common problem that this library usually gives is not respecting the accents, that we can solve with a function to then review each text of the content.

 <? function accents ($ string) { $ search = explode (",", "á, é, í, ó, ú, ñ, Á, É, Í, Ó, Ú, Ñ, á, à ©, Ã, à & sup3;, ú, à ±, Ã, Ã, Ã, Ã, Ã, Ã,Ã, Ã,Ã,Ã, Ã,Ã, Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ãâ,¬Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,Ã,à $ change = explode (",", "á, é, í, ó, ú, ñ, Á, É, Í, Ó, Ú, Ñ, á, é, í, ó, ú, ñ, Á, É, Í, Ó, Ú, Ñ "); $ correctedtext = str_replace ($ search $ change, $ string); return $ corrected text; }?> 

In this way we will save the problem of FPDF and the accents.

by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked Jun 23, 2019 in PHP by backtothefuture (551k points) | 238 views
+4 votes
1 answer
asked Oct 4, 2019 in PHP by backtothefuture (551k points) | 1.5k views
+4 votes
1 answer
asked Apr 27, 2020 in PHP by backtothefuture (551k points) | 1.2k views
+5 votes
1 answer
asked Oct 6, 2019 in PHP by backtothefuture (551k points) | 1.6k views
+5 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users