Saturday, 28 April 2018

Codeigniter PDF Generation using Dompdf


We all know PDF document is the extremely helpful type for make web document in any website. This type of file give us a very simple and user interface friendly style for download large amount of data in single file. If we want to download web data, then first we want to convert that data from HTML to PDF format. So, if we want to make conversion from HTML to PDF format, for this we have to use PHP Library.

PHP has one Dompdf library which can make PDF file from HTML content. By using this library it is very simple to generate PDF file from HTML content in PHP script using Dompdf library. But if you have use Codeigniter framework for web development, So there is somethings different process for generate PDF file using Dompdf in Codeigniter. Here in this post we have disccuss how to convert HTML content to PDF format file or generate PDF file in codeigniter by using Dompdf.

Dompdf library provides us to simplest way to generate PDF from HTML in Codeigniter. Here this example will show you how to configure Dompdf library in Codeigniter. Here we will make Codeigniter PDF library by using this Dompdf library and after this we can use Codeigniter library as we have use other in build Codeigniter library. In this Customer PDF library we can use all the available feature of Dompdf library class which we can use in our customer Codeigniter PDF library. Below example we have generates dynamic PDF file from dynamic HTML content. So by using this code we can generate dynamic PDF files from any dynamic HTML data as per our requirement.






Source Code


application/controllers/HtmltoPDF.php



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

class HtmltoPDF extends CI_Controller {

 public function __construct()
 {
  parent::__construct();
  $this->load->model('htmltopdf_model');
  $this->load->library('pdf');
 }

 public function index()
 {
  $data['customer_data'] = $this->htmltopdf_model->fetch();
  $this->load->view('htmltopdf', $data);
 }

 public function details()
 {
  if($this->uri->segment(3))
  {
   $customer_id = $this->uri->segment(3);
   $data['customer_details'] = $this->htmltopdf_model->fetch_single_details($customer_id);
   $this->load->view('htmltopdf', $data);
  }
 }

 public function pdfdetails()
 {
  if($this->uri->segment(3))
  {
   $customer_id = $this->uri->segment(3);
   $html_content = '<h3 align="center">Convert HTML to PDF in CodeIgniter using Dompdf</h3>';
   $html_content .= $this->htmltopdf_model->fetch_single_details($customer_id);
   $this->pdf->loadHtml($html_content);
   $this->pdf->render();
   $this->pdf->stream("".$customer_id.".pdf", array("Attachment"=>0));
  }
 }

}

?>



application/models/Htmltopdf_model.php



<?php
class Htmltopdf_model extends CI_Model
{
 function fetch()
 {
  $this->db->order_by('CustomerID', 'DESC');
  return $this->db->get('tbl_customer');
 }
 function fetch_single_details($customer_id)
 {
  $this->db->where('CustomerID', $customer_id);
  $data = $this->db->get('tbl_customer');
  $output = '<table width="100%" cellspacing="5" cellpadding="5">';
  foreach($data->result() as $row)
  {
   $output .= '
   <tr>
    <td width="25%"><img src="'.base_url().'images/'.$row->images.'" /></td>
    <td width="75%">
     <p><b>Name : </b>'.$row->CustomerName.'</p>
     <p><b>Address : </b>'.$row->Address.'</p>
     <p><b>City : </b>'.$row->City.'</p>
     <p><b>Postal Code : </b>'.$row->PostalCode.'</p>
     <p><b>Country : </b> '.$row->Country.' </p>
    </td>
   </tr>
   ';
  }
  $output .= '
  <tr>
   <td colspan="2" align="center"><a href="'.base_url().'htmltopdf" class="btn btn-primary">Back</a></td>
  </tr>
  ';
  $output .= '</table>';
  return $output;
 }
}

?>


application/views/htmltopdf.php



<html>
<head>
    <title>Convert HTML to PDF in CodeIgniter using Dompdf</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
 <div class="container box">
  <br />
  <h3 align="center">Convert HTML to PDF in CodeIgniter using Dompdf</h3>
  <br />
  <?php
  if(isset($customer_data))
  {
  ?>
  <div class="table-responsive">
   <table class="table table-striped table-bordered">
    <tr>
     <th>Customer ID</th>
     <th>Customer Name</th>
     <th>View</th>
     <th>View in PDF</th>
    </tr>
   <?php
   foreach($customer_data->result() as $row)
   {
    echo '
    <tr>
     <td>'.$row->CustomerID.'</td>
     <td>'.$row->CustomerName.'</td>
     <td><a href="'.base_url().'htmltopdf/details/'.$row->CustomerID.'">View</a></td>
     <td><a href="'.base_url().'htmltopdf/pdfdetails/'.$row->CustomerID.'">View in PDF</a></td>
    </tr>
    ';
   }
   ?>
   </table>
  </div>
  <?php
  }
  if(isset($customer_details))
  {
   echo $customer_details;
  }
  ?>
 </div>
</body>
</html>


application/libraries/Pdf.php



<?php 
if (!defined('BASEPATH')) exit('No direct script access allowed');  
 
require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

class Pdf extends Dompdf
{
 public function __construct()
 {
   parent::__construct();
 } 
}

?>





2 comments: