How to Configure domPdf with Codeigniter

How to Configure domPdf with Codeigniter

In this Post, I would like to present how to use domPdf library with codeigniter to generate PDF reports. The HTML to PDF conversion can be easily done using domPdf PHP library.

What is domPdf ?

DomPdf is a PHP library that produces PDF from HTML content. To generate reports in pdf we can use dompdf written in PHP.

Key Features:

a.) Support external stylesheets.
b.) Support complex tables.
c.) Image Support.
d.) Inline PHP support.


  • PHP version 7.1 or higher
  • DOM extension
  • MBString extension
  • php-font-lib
  • php-svg-lib
  • GD Extension (For image processing)

I will show you step by step dompdf setup with codeigniter.I am assuming that you already know about setup of codeigniter.If you have any issue with codeigniter setup then you can check this post Codeigniter Setup.

In this tutorial I am using codeigniter 3.1.9 version.

1.) Download dompdf library. Unzip inside myapp/application/libraries.

2.) Next create class pdf.php inside myapp/application/libraries directory to  initiate dompdf library.Copy below script.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
// Dompdf namespace use Dompdf\Dompdf;
class Pdf{ publicfunction __construct(){
require_once dirname(__FILE__).'/dompdf/';
$pdf = new DOMPDF(); $CI = & get_instance();
$CI->dompdf = $pdf; 
} } ?>

3.) Next, create a controller inside application/controller directory.And get all user details from database table tbl_user using codeigniter active record.


<?php defined('BASEPATH') OR exit('No direct script access allowed');
class PdfController extends CI_Controller {
publicfunction index(){
$data_user = array();
$data_user['users'] = $this->db->get('tbl_user')->result();
$html = $this->output->get_output();
$this->dompdf->setPaper('A4', 'landscape');
$this->dompdf->stream("welcome.pdf", array("Attachment"=>0));

4.) Create view to show output as a dompdf view.


<title>User Report</title>
font-family: Verdana; 
font-size: 18pt; 
font-style: normal; 
font-weight: bold; 
text-align: center; 
font-family: Verdana; 
font-size: 12pt; 
font-style: normal;
font-weight: bold;
border-collapse: collapse; 
<h3>Users Report</h3>
    <table align="center" cellpadding = "5" border="1">
<?php foreach($users as $row) { ?>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->mobile; ?></td>
<td><?php echo $row->address; ?></td>
<?php } ?>

5.) Now, go to browser and run http://localhost/myapp/pdf. You will get below screen .

Display Images

To display remote url images in pdf you need to set true remote enabled in dompdf options.Find isRemoteEnabled option in the file named options.php located in dompdf/src and set True.

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!