Search

How to Download Html to Pdf in Codeigniter Using Dompdf Library

  • Share this:
post-title

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 like ecommerce,booking,billing and etc. Pdf file provides a easy and user-friendly way to download the bunch of records in a pdf format file.

What is Dompdf ?

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.

Install with composer Dompdf

composer require dompdf/dompdf

Features Dompdf

  • 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 (see "Limitations" below)

Requirements

  • PHP version 7.1 or higher
  • DOM extension
  • MBString extension
  • php-font-lib
  • php-svg-lib

Step 1: Download Fresh Codeigniter 3

In first step we have need to download fresh codeigniter new version of codeigniter 3.1.11, after download successfully, extract clean new codeigniter 3 application.

Create Pdf.php file under “application/libraries” folder and paste the following code

<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter PDF Library
 *
 * @package			CodeIgniter
 * @subpackage		Libraries
 * @category		Libraries
 * @author			ADIL KHAN AJAD
 * @license			MIT License
 *
 */

require_once(dirname(__FILE__) . '/autoload.inc.php');
use Dompdf\Dompdf;

class Pdf
{
	public function create($html,$filename,$Attachment = true)
    {   
        
	    $dompdf = new Dompdf(array('enable_remote' => true));
	    $dompdf->loadHtml($html);
	    $dompdf->render();
	    $dompdf->stream($filename.'.pdf',array("Attachment" => $Attachment));
  }
}

Step 3: Add Controller Method

In this step we require to add “view_as_pdf” and “download_as_pdf” method on welcome controller, So let’s add with following code. you have to just copy of welcome.php controller file:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/*
	|=================================================
	|@Author K.K ADIL KHAN AJAD
	|=================================================
	|@Date 02-02-2022
	|=================================================
	|@Help [email protected]
	|=================================================
	*/
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index(){
		$this->load->view('welcome_message');
	}

	/**
    * Get view PDF File
    *
    * @return Response
   */
	public function view_as_pdf(){
		$this->load->library('dompdf/pdf');
        $filename = "pdf-".date('Y_m_d_H_i_s');
        $this->data['data'] = '';
        $html = $this->load->view('welcome_message',$this->data,true);
        $is_download = false;
        $this->pdf->create($html, $filename,$is_download);
	}
	/**
    * Get Download PDF File
    *
    * @return Response
   */
	public function download_as_pdf(){
		$this->load->library('dompdf/pdf');
        $filename = "pdf-".date('Y_m_d_H_i_s');
        $this->data['data'] = '';
        $html = $this->load->view('welcome_message',$this->data,true);

        $is_download = true;
        $this->pdf->create($html, $filename,$is_download);
	}
}

Step 4: Add View File

Now at last step we require to load html view file for generate pdf file.  So you have to copy bellow code and create on view folder:

we have loaded same codeigniter welcome page 

Now you can open bellow URL on your browser:

http://127.0.0.1/html-to-pdf/

GitHub URL https://github.com/Stacksolution/html-to-pdf-codeigniter