Search code examples
codeigniterpdf-generationamchartshtml2pdf

Generating a PDF Report from my Web Application in CodeIgniter


I have developed a web application in CodeIgniter which contains a good number of Functionalities such as: drawing charts using the JavaScript library Amcharts, implementing an Offline Map for data presentation and many more. What I want now is to be able to generate a PDF report for my pages, which contains some charts and a description of the chart when a user clicks on a button. I have searched for many libraries such as HTML2PDF, jsPDF, but none of these libraries seem to meet my needs. A screenshot of my page is shown here. I really need recommendations on libraries which could help do what I want, if anybody has been in such a situation.


Solution

  • Try Dompdf. Easy to use and works well with CodeIgniter. We've used this for a couple of our customers without any problems.

    https://github.com/dompdf/dompdf

    Make sure to download 0.6.x the 0.7 and higher won't work since it uses namespaces.

    To use this make a helper function like so:

    defined('BASEPATH') OR exit('No direct script access allowed');
    
    function pdf_create($html, $filename = 'example', $stream = true) 
    {
        require_once('dompdf/dompdf_config.inc.php');
    
        $dompdf = new DOMPDF();
    
        $dompdf->load_html($html);
    
        $dompdf->render();
    
        if ($stream) {
            $dompdf->stream("$filename.pdf");
        } else {
            return $dompdf->output();
        }
    }
    

    and in your controller you would call it like so:

    $this->load->helper('dompdf');
    
    pdf_create('Hello!');