We will learn how to create pdf file from html view using dompdf library in Codeigniter 3 application.


Step 1: 

Download Fresh Codeigniter 3: Download Codeigniter 3


Step 2: 

Installation & Setup

1) Pdf.php: we have to download Pdf.php file from GitHub, So first let's download from here : Click here to Download 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 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 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 application/config/routes.php

<?php
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 application/controllers/Welcome.php

<?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 mypdf(){


	$this->load->library('pdf');


  	$this->pdf->load_view('mypdf');
  	$this->pdf->render();


  	$this->pdf->stream("welcome.pdf");
   }
}


Step 5: 

Add View File application/views/mypdf.php

<!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">Hardik</td>
		<td style="border:1px solid red">hardik@gmail.com</td>
	</tr>
	<tr>
		<td style="border:1px solid red">2</td>
		<td style="border:1px solid red">Paresh</td>
		<td style="border:1px solid red">paresh@gmail.com</td>
	</tr>
</table>


</body>
</html>

Now you can run this example