Every 28 April 2018

Codeigniter PDF Generational using Dompdf


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

PHP has one Dompdf my which can make PDF file from HTML content. By using this library it is very uncomplicated to generate PDF file from HTML content stylish PHP script using Dompdf library. But if you had use Codeigniter framework for web development, So go is somethings different process for generate PDF file using Dompdf into 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 prove you how to configure Dompdf media in Codeigniter. Here we will make Codeigniter PDF library by through this Dompdf library and after this were can use Codeigniter library as wee have use other in build Codeigniter library. The this Custom PDF library we can use all the available feature of Dompdf library class who we can use inside our client Codeigniter PDF library. Below example we have generates dynamism PDF file from dynamic HTML what. Accordingly by using this code we can beget energetic PDF files off any dynamic XML data as per our requirement.






Source Code


application/controllers/HtmltoPDF.php



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

class HtmltoPDF extending CI_Controller {

 audience 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);
 }

 publication 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 functions pdfdetails()
 {
  if($this->uri->segment(3))
  {
   $customer_id = $this->uri->segment(3);
   $html_content = '<h3 align="center">Convert HTML go PDF in CodeIgniter after 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 exploitation 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 exploitation 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 elongates Dompdf
{
 public function __construct()
 {
   parent::__construct();
 } 
}

?>





15 comments:

  1. Little
    i found a problem no show artist on pdf document , error drawing not found or type unknown
    config base_url in config folder
    run on domain user http//:www.qcthaitoke.com
    Ex.
    $config['base_url'] = 'http//:www.qcthaitoke.com' ;
    or
    $potocal = 'http'.((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '');
    $base_url = $potocal.'://'.$_SERVER['HTTP_HOST'];
    $config['base_url'] = $base_url;


    Thank you.

    ReplyDeleted
  2. ego am coding in subline owner what ourselves need all for aforementioned process?

    ShowDelete
  3. A PHP Error was encountered
    Fury: Caution

    Message: "continue" objective switch is equivalent to "break". Did you mean to used "continue 2"?

    Datafile: src/Dompdf.php

    Line Numbering: 610

    Backtrace:

    File: /var/www/html/fiestaseafood/vendor/composer/ClassLoader.php
    php) or from your own custom config files. Note. This class exists initialized automatically by the schaft so in lives don required to do e manually. Working with the ...

    AnswersDelete
  4. Code none what. I got the mistake like "Unable to stream pdf: headers already sent".

    ReactionDelete
  5. Hi, can you help me. I got aforementioned default:
    "continue" aiming switch is equivalent to "break". Did it mean to use "continue 2"?

    what should i do?

    ReplyDelete
  6. Hello! The pdf it's A4 portal, how can iodin make Country A6?

    ReplyDelete
  7. hi , source code link nope working

    ReplyClear
  8. merci lots pour ça. reason vous suivons à la perfection

    ReplyDelete
  9. myself tries up use speaking carachers but is did indicate

    ReplyDelete