The Pdf file creation in PHP mainly requires when need to generate the file on the basis of the available data otherwise, simply create it manually with the external applications.
For example – generating the report, the user certificate, etc.
For generating pdf file I am using Dompdf
library which generates the downloadable pdf file from HTML.
Contents
1. Basic example
Download the library from here.
Store HTML string in $html
and assign file name in $filename
.
Include autoload.inc.php
.
Instantiating Dompdf
Class object and pass $html in loadHtml()
method. The render()
method renders the HTML to pdf.
Completed Code
<?php $html = "<table border='1' width='100%' style='border-collapse: collapse;'> <tr> <th>Username</th><th>Email</th> </tr> <tr> <td>yssyogesh</td> <td>yogesh@mintcream-cheetah-602163.hostingersite.com</td> </tr> <tr> <td>sonarika</td> <td>sonarika@mintcream-cheetah-602163.hostingersite.com</td> </tr> <tr> <td>vishal</td> <td>vishal@mintcream-cheetah-602163.hostingersite.com</td> </tr> </table>"; $filename = "newpdffile"; // include autoloader require_once 'dompdf/autoload.inc.php'; // reference the Dompdf namespace use Dompdf\Dompdf; // instantiate and use the dompdf class $dompdf = new Dompdf(); $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream($filename);
2. Preview of the file
For displaying the preview of the file before it is being downloaded. You need to pass the second parameter in the stream()
method which contains Attachment
option name in the Array.
By default, Attachment
option value is 1 which forces the browser to open the download dialog. So, instead of 1 pass 0.
Completed Code
<?php $html = "<table border='1' width='100%' style='border-collapse: collapse;'> <tr> <th>Username</th><th>Email</th> </tr> <tr> <td>yssyogesh</td> <td>yogesh@mintcream-cheetah-602163.hostingersite.com</td> </tr> <tr> <td>sonarika</td> <td>sonarika@mintcream-cheetah-602163.hostingersite.com</td> </tr> <tr> <td>vishal</td> <td>vishal@mintcream-cheetah-602163.hostingersite.com</td> </tr> </table>"; $filename = "newpdffile"; // include autoloader require_once 'dompdf/autoload.inc.php'; // reference the Dompdf namespace use Dompdf\Dompdf; // instantiate and use the dompdf class $dompdf = new Dompdf(); $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream($filename,array("Attachment"=>0));
3. Save to server
Saving the generated file on the server for this executing output()
method after the render()
and store return value in a variable($output) and writing it in a file using file_put_contents() method.
Completed Code
<?php $html = "<table border='1' width='100%' style='border-collapse: collapse;'> <tr> <th>Username</th><th>Email</th> </tr> <tr> <td>yssyogesh</td> <td>yogesh@mintcream-cheetah-602163.hostingersite.com</td> </tr> <tr> <td>sonarika</td> <td>sonarika@mintcream-cheetah-602163.hostingersite.com</td> </tr> <tr> <td>vishal</td> <td>vishal@mintcream-cheetah-602163.hostingersite.com</td> </tr> </table>"; // include autoloader require_once 'dompdf/autoload.inc.php'; // reference the Dompdf namespace use Dompdf\Dompdf; // instantiate and use the dompdf class $dompdf = new Dompdf(); $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); $output = $dompdf->output(); file_put_contents("file.pdf", $output);
4. Conclusion
You can easily use this library in your project when you have to generate the pdf file. You need to create your HTML layout and store in a variable or you can directly pass it in loadHtml().
It has some limitations that you need to know –
- Not particularly tolerant to poorly-formed HTML input (using Tidy first may help).
- Large files or large tables can take a while to render.
- CSS float is not supported (but is in the works).
You can learn more about this from their documentation with the examples.
If you found this tutorial helpful then don't forget to share.
Thank you.
This is what I’ve been looking for
Steve
I want to know that how can i make a pdf of php table. The example you have given for a html static table. I am looking forward dynamic table. i can’t get what to do.
now this things create a pdf with default padding.
1. How to remove that?
2. how to set page break in this method of creating pdf using dompdf?
Unable to locate the specified class: Session.php error is getting.
Hi, can you suggest me how to use image, i am trying to show image using (mg tag) but not visible.
Hi, how do you do paging with dompdf?
Scusi ho un problema:
Il programma DOMPdf funziona egregiamente ma commette un errore che non riesco a superare:
la verisone pdf è alquanto diversa dalla versione html della pagina.
In particolare le tabelle appaiono, nella versione pdf, del tutto diverse con le righe molto piu’ grandi del necessario sia in larghezza che in altezza rispetto a quelle che compaiono nella versione html del file.
Qualche idea?
PS – Vorrei inviare lo script del file html ma non vedo come fare in quanto lo stesso è discretamente lunto.
Lo copio qui
Gestione Cassa
Croce d’Oro Montale – Montale (PT)
20^ Festa
dal 01/02/2020 al 28/02/2020 (27 giorni))
Servizio Cucina, Pizzeria e Bar
Cassa Centrale
Ricevuta Ordinativo n. 25
Prodotto
Quantita’
Costo unitario
Prezzo
SERVIZIO
6
E. 0,50
E. 3,00
PIZZA 4 STAGIONI
6
E. 5,50
E. 33,00
ANATRA IN UMIDO
6
E. 7,00
E. 42,00
Totale
E. 78,00
Pag. n. 1
Buon Appetito!
Montale – 16/03/2020 – 15:03:33
Copia per il Contribuente
Croce d’Oro Montale – Montale (PT)
20^ Festa
dal 01/02/2020 al 28/02/2020 (27 giorni))
Servizio Cucina, Pizzeria e Bar
Cassa Centrale
Ricevuta Ordinativo n. 25
Prodotto
Quantita’
Costo unitario
Prezzo
<p align = 'left'
PIZZA 4 STAGIONI
6
E. 5,50
E. 33,00
Pag. n. 1
Copia per la Struttura
PIZZERIA
a nome del Contribuente
Croce d’Oro Montale – Montale (PT)
20^ Festa
dal 01/02/2020 al 28/02/2020 (27 giorni))
Servizio Cucina, Pizzeria e Bar
Cassa Centrale
Ricevuta Ordinativo n. 25
Prodotto
Quantita’
Costo unitario
Prezzo
<p align = 'left'
ANATRA IN UMIDO
6
E. 7,00
E. 42,00
Pag. n. 1
Copia per la Struttura
CUCINA
a nome del Contribuente
Thank you very much !!!!!
if it supports bootstrap 4 by using cdn?
hello yogesh can i create and save pdf file
Как подключить css стили?
how to connect styles css?
hi.. can i use image of Longblob data from database to create pdf file