Online Snippets Library

DomPDf Integration In Codeigniter With Example

Dompdf is an PHP class for generating pdf documents from html.Dompdf is free to use.we will use following step for DomPDf integration with Codeigniter.

Step 1

To Download DomPDf Click Here.

Step 2

Unzip the above download inside application/libraries/dompdf.

Step 3

Create a new file inside application/libraries/Pdf.php

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
use Dompdf\Dompdf;
class Pdf extends DOMPDF
{
    function __construct()
    {
        parent::__construct();
    }
}
/*Author:Tutsway.com */  
/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */
Step 4

Create Controller file inside application/controllers/dompdf.php.

<?php 
class dompdf extends CI_Controller{
      function __construct() { 
 parent::__construct();
 } 
 function index()
 {
 $this->load->library('pdf');
 $dompdf = new Dompdf\Dompdf();y
 // Set Font Style
 $dompdf->set_option('defaultFont', 'Courier');
 $html = "<p style='text-align: center'>My First Dom Pdf Example</p>";
 $dompdf->loadHtml($html);
 // To 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("My.pdf");
 }
}
?>