How to generate PDF from Codeigniter view using mPDF

 It is very essential to generate PDF file in most of the web applications. mPDF is an open source PHP Library to generate PDF files from view in Codeigniter. Now follow the below steps.


 

Step 1: Download mPDF PHP Library from the link and extract it... DOWNLOAD MPDF 

Step 2: Copy "mpdf" directory from "third_party" folder under "application" and paste it in   

             your Codeigniter project's "third_party" directory.

Step 3: Copy "Pdf.php" file directory from "library" folder and paste it in   

             your project's same directory. Now edit this file and paste the following script in it.

        <?php
        function mypdf() {
            $this->load->library('pdf');
            $pdf = $this->pdf->load();
            $html = $this->load->view('myPdf_view', null, true);
            $pdf->WriteHTML($html);
            // write the HTML into the PDF
            $output = 'itemreport' . rand(10,100) . '_.pdf';
            $pdf->Output("$output", 'I');
        }
        ?>

Step 4: Now create a view 'myPdf_view' with required content.    

Step 5: Finally call your controller with mypdf() method as follows.

www.mywebsite.com/myController/mypdf

It will generate PDF file with the content of  myPdf_view. Happy Coding...

 

 

 

Previous
Next Post »
Related Posts Plugin for WordPress, Blogger...