Convert HTML to PDF in CodeIgniter using Dompdf

PDF is the most used format to create the document in the web application. PDF file provides a simple and user-friendly way to download the bunch of data in a file. Before download the web page content, the content needs to be converted from HTML to PDF. The HTML to PDF conversion can be easily done using PHP library.

Dompdf is a PHP library that helps to generate PDF from HTML content. It’s very easy to convert HTML to PDF in PHP with Dompdf. If your application built with CodeIgniter, a PDF library needs to be created to generate PDF using Dompdf. In this tutorial, we will show you how to convert HTML to pdf and generate PDF using Dompdf in CodeIgniter.

Controller

The index() function of Welcome controller generates PDF from HTML.

  • Get output HTML from welcome_message view.
  • Load CodeIgniter Pdf library to use Dompdf class.
  • Convert HTML content and generate PDF using Dompdf.
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {
    
    public function 
index(){
        
$this->load->view('welcome_message');
        
        
// Get output html
        
$html $this->output->get_output();
        
        
// Load pdf library
        
$this->load->library('pdf');
        
        
// Load HTML content
        
$this->dompdf->loadHtml($html);
        
        
// (Optional) Setup the paper size and orientation
        
$this->dompdf->setPaper('A4''landscape');
        
        
// Render the HTML as PDF
        
$this->dompdf->render();
        
        
// Output the generated PDF (1 = download and 0 = preview)
        
$this->dompdf->stream("welcome.pdf", array("Attachment"=>0));
    }
    
}

Library

CodeIgniter PDF library (Pdf.php):
The CodeIgniter PDF library is a custom library that helps to convert HTML output to PDF using DOMPDF.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * CodeIgniter PDF Library
 *
 * Generate PDF in CodeIgniter applications.
 *
 * @package            CodeIgniter
 * @subpackage        Libraries
 * @category        Libraries
 * @author            CodexWorld
 * @license            https://www.codexworld.com/license/
 * @link            https://www.codexworld.com
 */

// 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;
        
    }
}
?>

Dompdf Library (/dompdf):
The custom PDF library uses Dompdf to generate PDF. So, include the Dompdf library to instantiate the Dompdf class.

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

Configuration Options

Dompdf library has various options to configure the PDF generation. In the example code, some most used configuration options (paper size and orientation, PDF output, etc.) are used. You can see all other available options from here.

Conclusion

Our custom Dompdf library provides the easiest way to convert HTML to PDF in CodeIgniter using Dompdf. Our example code shows the most useful configuration options to generate PDF in CodeIgniter. All the available functionality of Dompdf class can be used in CodeIgniter PDF library. The example code generates the PDF from HTML of the view. You can convert any dynamic HTML and generate PDF as per your needs.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

5 Comments

  1. Mandeep Bagria Said...
  2. Tebogo Said...
  3. Chirag Said...
  4. Mohan Said...
  5. Programming Pot Said...

Leave a reply

keyboard_double_arrow_up