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



  • Pedro Bezunartea López
    And? Where's the rest? how do you manage paging?
    January 6, 2016
    Reply
    • admin
      info@webeasystep.com
      hi Mr.Perdo this is the full tutorial to demonstrate how to use this library ,and no rest,but if you want more details for other features in this library you can read documentation it is very clear and helpful about your question you can use this line to manage page beak $mpdf->AddPage(); you can also enable or disable page break for more details see this link http://www.mpdf1.com/mpdf/index.php
      January 9, 2016
      Reply
  • 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 23, 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
  • alvin
    how to generate pdf on specific div only using mpdf
    April 6, 2017
    Reply
    • admin
      info@webeasystep.com
      as you saw in this tutorial ,we loaded itemreport/plist_item view file which contains html php code, so you can put your div into the view file , gave it a specific class or custom style , then when MPDF render the file you will show your style
      April 7, 2017
      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

Leave a Reply

Your email address will not be published.


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