In this tutorial, we will discuss how to generate PDF using DOMPDF with Codeigniter Framework version 3 and composer(PHP dependency manager).
From the authors of Dompdf –
Dompdf is an HTML to PDF converter. At its heart, DomPDF is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
- Handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
- Supports most presentational HTML 4.0 attributes
- Supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
- Supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling
- Image support (gif, png (8, 24 and 32 bit with alpha channel), bmp & jpeg)
- No dependencies on external PDF libraries, thanks to the R&OS PDF class
- Inline PHP support
- Basic SVG support
- PHP version 5.3.0 or higher
- DOM extension
- GD extension
- MBString extension
This tutorial assuming –
1. you have already installed working CodeIgniter application.
2. Basic knowledge of CodeIgniter and composer.
To install DomPDF with Composer, simply require the latest version of this package as shown below from your project root folder where composer.json reside.
composer require dompdf/dompdf:[email protected]
Now enable composer auto loading by making changes to config.php file. Default value was FALSE change it to TRUE.
$config['composer_autoload'] = TRUE;
Wow! everything is ready. As we completed required installation steps, now you can use DomPDF in your controllers like below shown.
load->view('welcome_message',,true); $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Get the generated PDF file contents $pdf = $dompdf->output(); // Output the generated PDF to Browser $dompdf->stream(); } }
Here “index” function of “Welcome” controller will generate pdf file of “welcome_message.php “view file.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.