Tutorials

Article Content:codeigniter tutorial pdf to create your reports

codeigniter tutorial pdf to create your reports

codeigniter tutorial pdf to create your reports.

UPDATE : Add MPDF 6  And Arabic Support 10/2017

From time to another with working as Web Developer Freelancer many clients want to add reports to their websites. And after many experiments I chose MPDF.

MPDF. is my best favorite library to deal with PDF reports files by PHP and in the following lines, I will explain why ?

1-  MPDF Has Good performance, so you don’t wait for a long time to render your report, this is will be so important if you work with big retrieved data from the database.

2- MPDF reduce PDF files size. so again don’t worry about you data size.

3- MPDF  Support many languages .and works without problems with RTL languages like Arabic,

4- MPDF Support CSS and improved to support css3 syntax, so you can design your beautiful report and let MPDF library convert it easily.

5- MPDF Support external TTF fonts ,just choose your favorite font and it will deal with

6- MPDF has well organized and clear documentation, user guide have many examples for every single code.

7- MPDF improved faster and it is free. So how to integrate this library with Codeigniter ?

Step one :

Download MPDF library and extract files in third_party directory

Step two :

Create new pdf.php file in libraries and put this code on it

class pdf {
 
    function pdf()
    {
        $CI = & get_instance();
        log_message('Debug', 'mPDF class is loaded.');
    }
 
    function load($param=NULL)
    {
        include_once APPPATH.'/third_party/mpdf/mpdf.php';
 
   
            $param = "'','', 0, '', 0, 0, 0, 0, 0, 0";

        return new mPDF($param);
    }
}

Step three :

Create new controller and put this function on it

 

    function print_item()
    {
       //     load library
        $this->load->library('pdf');
        $pdf = $this->pdf->load();
       // retrieve data from model
        $data['all_itemreport'] = $this->itemreport->get_items();
        $data['title'] = "items";
        ini_set('memory_limit', '256M'); 
       // boost the memory limit if it's low ;)
        $html = $this->load->view('itemreport/plist_item', $data, true);
       // render the view into HTML
        $pdf->WriteHTML($html); // write the HTML into the PDF
        $output = 'itemreport' . date('Y_m_d_H_i_s') . '_.pdf';
        $pdf->Output("$output", 'I'); // save to file because we can
        exit();
    }

Step four :

Create new model and put this function to select data from database

 

function get_items()
{
    $this->db->order_by("it_created", "asc");
    $result = $this->db->get("d_items");
    return $result->result_array();
}

Step five :

Create new view file to load HTML report and put table.

<h2>News Table</h2>
<div class="alert alert-info">
    <strong>Demo Test!</strong>
    You can test MPDF library in this demo with latest version</div>
<p></p>
<div class="table-responsive">
<table class="table">
    <thead>
    <tr>
        <th>Title</th>
        <th>Desc</th>
        <th>Image</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($news as $n) : ?>
    <tr>
        <td><?= $n['ne_title']; ?></td>
        <td><?= $n['ne_desc']; ?></td>
        <td><img src="<?php echo base_url(); ?>global/uploads/<?= $n['ne_img']; ?>"/>
        </td>
    </tr>
    </tbody>
    <?php endforeach ?>
</table>
</div>

Additional options :

to support arabic or any utf-8 languages add these three lines to step three (TESTED ON Version 6 only )

$pdf->allow_charset_conversion=true;  // Set by default to TRUE
$pdf->charset_in='UTF-8';
$pdf->SetDirectionality('rtl');
$pdf->autoLangToFont = true;

then add your lang code to the html tag as example for arabic language add ar

<html lang="ar">

Download Full Code



  • krishna
    how to add header
    May 28, 2016
    Reply
    • admin
      info@webeasystep.com
      you can set header by 4 methods , the most popular solution is to use SetHeader() or SetHTMLHeader() for more details,see this page https://mpdf.github.io/headers-footers/headers-footers.html
      May 28, 2016
      Reply
  • azka
    ada tutorial dalam bentuk videonya mas? terimakasih..
    October 24, 2016
    Reply
  • Albert
    No database? Source doesn´t have one. Please provide database. Thank you.
    November 1, 2016
    Reply
    • admin
      info@webeasystep.com
      No, it is included,just follow steps in this youtube video and it will work correctly thank you
      November 1, 2016
      Reply
  • jayesh
    hello sir.. in my localhost codeigniter working .. but in live website found that error.. TCPDF ERROR: Some data has already been output to browser, can't send PDF file so please help me and send replay in mail.. Thank you
    July 4, 2017
    Reply
  • Nithin P Varghese
    Can you please say how to give paper size in this code. Iam using epson LX310. its paper size is big. so need to print clearly in it. by this code print is not clear. how to make the print clear ?
    October 11, 2017
    Reply
    • admin
      info@webeasystep.com
      you can change print size from pdf library constructor like this $param = "'mode' => 'utf-8', 'format' => 'A4-L', 0, '', 0, 0, 0, 0, 0, 0"; for more info follow this page https://mpdf.github.io/paging/page-size-orientation.html
      October 15, 2017
      Reply
  • Sourav Mallick
    Severity: 8192 Message: Methods with the same name as their class will not be constructors in a future version of PHP; pdf has a deprecated constructor
    October 27, 2017
    Reply
  • Antony
    What to do? Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in C:\xampp\htdocs\mpdf-codeigniter\application\config\config.php on line 355
    February 9, 2018
    Reply
    • admin
      info@webeasystep.com
      it is works fine until 5.6 but if you want to use php v 7+ you should upgrade the library by using the latest version of php mpdf library.
      February 12, 2018
      Reply
  • Cinta Dewi Amelia
    I have followed the steps as per your instructions yes successfully to print to pdf, but when the query in my model i add parameter, i getting error 404, why? how to solve it? thanks
    February 19, 2018
    Reply
  • vinay
    i am getting error mPDF error: Some data has already been output to browser, can't send PDF file
    April 24, 2018
    Reply
  • Debraj Chatterjee
    hi, i have followed all steps. But, Myy pdf is somewhat destorted and comes in 2 pages. how can i adjust the layout. please help
    April 29, 2018
    Reply
  • Sunil Patro
    how to start pdf print on USB printer once server started port is able to access
    July 9, 2019
    Reply
  • Kumud Kanth
    class pdf { function pdf() { $CI = & get_instance(); log_message('Debug', 'mPDF class is loaded.'); } function load($param=NULL) { include_once APPPATH.'/third_party/mpdf/mpdf.php'; $param = "'','', 0, '', 0, 0, 0, 0, 0, 0"; return new mPDF($param); } } please help me solve error in thermal printer
    June 22, 2020
    Reply
  • Gyanendra singh
    Nice tutor
    November 19, 2020
    Reply

Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.