Codeigniter Generate PDF from View using Dompdf Example

By Hardik Savani November 5, 2023 Category : PHP HTML Codeigniter

In this tutorial, i am going to share with you how to create pdf file from html view using dompdf library in Codeigniter 3 application. We will integrate dompdf library from scratch so no issue if you don't know about dompdf library.

We generally require to generate PDF for invoice, data, information, subscription etc. If you use any PHP framework like laravel, code php, yii etc then you have option to other package for generate HTML file into PDF. But if you are working on Codeigniter then you fetch problem to how to do it. But in this article i am going to give you very simple example to html to pdf convert using DomPDF library.

DomPDF library through we can simply render html layout into PDF file. DomPDF library also allow to write external style sheets, inline style tags, font size, font color etc. DomPDF will help to do customize PDF file. Dompdf library is available on composer package so if you are working with composer then also you can use it.

So, i am going to share with you example of how to generate PDF file from HTML layout using DomPDF library, If you don't know how to implement it then no worry because i am doing from scratch and you have to just follow few step to create PDF file.

Step 1: Download Fresh Codeigniter 3

In First step we will download fresh version of Codeigniter 3, so if you haven't download yet then download from here : Download Codeigniter 3.

After Download successfully, extract clean new Codeigniter 3 application.

Step 2: Installation & Setup

In first step we have to download one library and one dependency of dompdf so follow bellow things.

1) Pdf.php: we have to download Pdf.php file from GitHub, So first let's download from here : Click Here to download Pdf.php file. After download successfully we have to copy to"application/libraries/Pdf.php" file.

2) Dompdf: we have to download dompdf library from GitHub, So first let's download from here : Click Here to download dompdf. After download extract it to your "application/libraries" folder and rename it to "dompdf".

3) php-font-lib: Ok, now Download php-font-lib from GitHub, So first let's download from here : Click Here to download php-font-lib classes. After download extract it to "application/libraries/dompdf/lib/php-font-lib/classes" folder. So you have all php font library it on classes folder.

Step 3: Add Route

In this step, we will add one route "mypdf" for demo, that way when we run this route we will download pdf file, So let's add following route on your routes.php file.



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

$route['default_controller'] = 'welcome';

$route['404_override'] = '';

$route['translate_uri_dashes'] = FALSE;

$route['mypdf'] = "welcome/mypdf";

Step 4: Add Controller Method

In this step we require to add "mypdf" method on welcome controller, So let's add with following code. you have to just copy of welcome.php controller file:



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()





* Get Download PDF File


* @return Response


function mypdf(){







Step 5: Add View File

Now at last step we require to create "mypdf.php" view file for generate pdf file. So now as bellow i created "mypdf.php" with some html code, so you have to copy bellow code and create on view folder:


<!DOCTYPE html>



<title>Codeigniter 3 - Generate PDF from view using dompdf library with example</title>



<h1>Codeigniter 3 - Generate PDF from view using dompdf library with example</h1>

<table style="border:1px solid red;width:100%;">


<th style="border:1px solid red">Id</th>

<th style="border:1px solid red">Name</th>

<th style="border:1px solid red">Email</th>



<td style="border:1px solid red">1</td>

<td style="border:1px solid red">Hardik</td>

<td style="border:1px solid red"></td>



<td style="border:1px solid red">2</td>

<td style="border:1px solid red">Paresh</td>

<td style="border:1px solid red"></td>





Ok, now we are ready to run our PDF generator example. So let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:


I hope it can help you...