In this article, You will learn about how to create pdf file in Codeigniter from html using dompdf library . PDF is the most used format to generate PDF for invoice in the web application. PDF file provides a easy and user-friendly way to download the bunch of records in a pdf format file. Before download the web data records content in pdf format, the content needs to be converted from HTML to PDF. If you are working on Codeigniter then you get problem to how to do it. But in this post i am going to share you very simple example to html to pdf convert using DomPDF library.
Dompdf is a PHP library that helps to generate PDF file from HTML data content. It’s very easy to convert HTML to PDF in PHP with Dompdf.
Step 1: Download Fresh Codeigniter 3
In First step we need to download fresh version of Codeigniter 3, After Download successfully, extract clean new Codeigniter 3 application.
so if you haven’t download yet then download from here : Download Codeigniter 3.
Step 2: Download dompdf library from GitHub
Dompdf: let’s download dompdf library from here : Click Here to download dompdf. After download, extract it to your “application/libraries” folder and rename it to “dompdf”.
Create pdf.php file under “application/libraries” folder and paste the following code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); // reference the Dompdf namespace use Dompdf\Dompdf; class Pdf { public function __construct(){ // include autoloader require_once dirname(__FILE__).'/dompdf/autoload.inc.php'; // instantiate and use the dompdf class $pdf = new DOMPDF(); $CI =& get_instance(); $CI->dompdf = $pdf; } } ?> |
Step 3: Add Controller Method
In this step we require to add “generatepdf” method on welcome controller, So let’s add with following code. you have to just copy of welcome.php controller file:
application/controllers/Welcome.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { /** * Get All Data from this method. * * @return Response */ public function index() { $this->load->view('welcome_message'); } /** * Get Download PDF File * * @return Response */ function convertpdf(){ // Get output html $html = $this->output->get_output(); // Load pdf library $this->load->library('pdf'); // Load HTML content $this->pdf->loadHtml($html); // (Optional) Setup the paper size and orientation $this->pdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $this->pdf->render(); // Output the generated PDF (1 = download and 0 = preview) $this->pdf->stream("welcome.pdf", array("Attachment"=>0)); } } |
Step-4: Add Route
let’s add following route on your routes.php file.
application/config/routes.php
2 3 4 5 6 7 8 9 10 11 12 13 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $route['default_controller'] = 'welcome'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; $route['generatepdf'] = "welcome/convertpdf"; |
Step 5: Add View File
Now at last step we require to create “generatepdf.php” view file for generate pdf file. So you have to copy bellow code and create on view folder:
application/views/generatepdf.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html> <html> <head> <title>Codeigniter 3 - Generate PDF from view using dompdf library with example</title> </head> <body> <h1>Codeigniter 3 - Generate PDF from view using dompdf library with example</h1> <table style="border:1px solid red;width:100%;"> <tr> <th style="border:1px solid red">Id</th> <th style="border:1px solid red">Name</th> <th style="border:1px solid red">Email</th> </tr> <tr> <td style="border:1px solid red">1</td> <td style="border:1px solid red">Jhon</td> <td style="border:1px solid red">jhon@gmail.com</td> </tr> <tr> <td style="border:1px solid red">2</td> <td style="border:1px solid red">Lucy</td> <td style="border:1px solid red">lucy@gmail.com</td> </tr> </table> </body> </html> |
Now you can open bellow URL on your browser:
http://localhost:8000/generatepdf
If you are on live sever:
http://domainname.com/generatepdf
Useful Methods of Dompdf
The following are some useful methods of Dompdf library to implement HTML to PDF conversion functionality.
- loadHtml(): Loads HTML content.
- $str (string) – Required. Specify the HTML to load.
- $encoding (string) – Optional. Specify encoding.
- loadHtmlFile(): Loads content from an HTML file.
- $file (string) – Required. Specify file path to load.
- output(): Returns the PDF as a string.
- $options (array) – Optional. Specify whether content stream compression will enable. (compress => 1 or 0)
- render(): Renders the HTML to PDF.
- setBasePath(): Sets the base path to include external stylesheets and images.
- $basePath (string) – The base path to be used when loading the external resources URLs.
- setPaper(): Sets the paper size & orientation.
- $size (string|array) – ‘letter’, ‘legal’, ‘A4’, etc.
- $orientation (string) – ‘portrait’ or ‘landscape’.
- stream(): Streams the PDF to the client.
- $filename (string) – Specify name of the file (without .pdf extension).
- $options (array) –
- ‘compress’ => 1 or 0 – enable content stream compression.
- ‘Attachment’ => 1 = download or 0 = preview
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co