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 full code example and video tutorial to CodeIgniter pdf generator

FIXED : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead. This happens with php5.5 mpdf

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

Download Full Code



  • Pedro Bezunartea López
    And? Where's the rest? how do you manage paging?
    January 6, 2016
    Reply
    • admin
      [email protected]
      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
      [email protected]
      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
      [email protected]
      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
      [email protected]
      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

Leave a Reply

Your email address will not be published.


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