This tutorial we are going to Generate PDF file using dompdf. As it’s provide a lot’s of inbuild functions to use directly in real life without do much effort. We use MPDF in our previous tutorial which mostly similar but this library have different functions. You can use it whatever you like most.
So let’s get started with simple example.
Download composer and install it. you can get it from github. If you want to use it on your existing project than it’s also works but make sure composer already installed on project root directory. below is proper and step by step execution of installation to create PDF file.
STEP 1 : Use below command to get dompdf lib from github.
composer require dompdf/dompdf
STEP 2 : Include autoload file make sure it’s loaded or not.
require('./vendor/autoload.php');
If you want use lib directly on project without compose being installed use autoload file and make sure path is correct.
require_once 'dompdf/autoload.inc.php';
STEP 3 : Let’s create index.php file and run it .
<?php require('./vendor/autoload.php'); // reference the Dompdf namespace use Dompdf\Dompdf; // instantiate and use the dompdf class $dompdf = new Dompdf(array('enable_remote' => true)); $html = "<table width='100%' style='border-collapse: collapse; table-layout:fixed;'> <tr> <td>Logo Here</td> <td style='text-align: -webkit-right;'> <table style='text-align:right;' align='right'> <tr><td>John Deo</td></tr> <tr><td>Germany</td></tr> <tr><td>Danmark, DE</td></tr> <tr><td>info@example.com</td></tr> </table> </td> </tr> </table>"; $html .= "<table width='100%' style='border-collapse: collapse; table-layout:fixed;'> <tr> <td><h3>Marcel Deo</h3></td> </tr> </table>"; $html .= "<table width='100%' style='border-collapse: collapse; table-layout:fixed;'> <tr> <td> <table style='font-size: 12px;'> <tr><td><strong>Date: </strong></td><td>2021-20-3</td></tr> <tr><td><strong>Client: </strong></td><td>pion</td></tr> </table> </td> <td></td> </tr> </table>"; $html .= "<table width='100%' style='border-collapse: collapse; table-layout:fixed;'> <tr> <td><br/> <p>Loren IPSUM</p> <p>Loren ipsim dummy text</p> data korne up sum drate st text<br/><br/> data korne up sum drate st textdata korne up sum drate st textdata korne up sum drate st textdata korne up sum drate st textdata korne up sum drate st text<br/><br/> data korne up sum drate st textdata korne up sum drate st textdata korne up sum drate st textdata korne up sum drate st text<br/><br/> </td> </tr></table>"; $customPaper = array(0,0,600,860); $dompdf->set_paper($customPaper); $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation //$dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); $output = $dompdf->output(); $dompdf->stream('dompdf.pdf',array("Attachment"=>0)); //$output = $dompdf->output(); // Output the generated PDF to Browser //$dompdf->stream(); ?>
$dompdf->set_paper($customPaper);
Set paper size as per needs, we can set custom width and height.
$dompdf->stream(‘dompdf.pdf’,array(“Attachment”=>0));
If attachment set is 0 which means render pdf file on browser without download AND if it’s set 1 download PDF file.
Limitations
- Not support Grid Layout.
- Support only inline CSS AND print PHP.
- It support external image path or base64 image
- You can only use table structure to make it more professional and readable.
Conclusion
We are created PDF file quickly and efficient way to achieve this. DOMPDF is powerful lib because it provided all require functions to make it simple and more user friendly. Developer Can use this directly on project and do require customizations. keep in mind that there is some limitations like image, inline style and inline PHP code other than this all good.