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.

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/autoload.inc.php';
$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.
PdfController.php
<?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();
$this->load->view('user_list',$data_user);
$html = $this->output->get_output();
$this->load->library('pdf');
$this->dompdf->loadHtml($html);
$this->dompdf->setPaper('A4', 'landscape');
$this->dompdf->render();
$this->dompdf->stream("welcome.pdf", array("Attachment"=>0));
}
} 
4.) Create view to show output as a dompdf view.
user_list.php
<html>
<head>
<title>User Report</title>
</head>
<body>
<style>
h3
{
font-family: Verdana; 
font-size: 18pt; 
font-style: normal; 
font-weight: bold; 
color:red;
text-align: center; 
}
table{
font-family: Verdana; 
color:black; 
font-size: 12pt; 
font-style: normal;
font-weight: bold;
text-align:left; 
border-collapse: collapse; 
}
</style>
<h3>Users Report</h3>
    <table align="center" cellpadding = "5" border="1">
<thead>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->mobile; ?></td>
<td><?php echo $row->address; ?></td>
</tr>
<?php } ?>
</tbody>
    </table>
</body>
</html>
5.) Now, go to browser and run http://localhost/myapp/pdf. You will get below screen .
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!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , ,