CodeIgniter Reporting with DOMPDF

Table of Contents

1 Introduction

I have been setting up a reporting framework in a CodeIgniter based project I am currently working on. I often see queries about setting up reporting for web-based applications so I thought I would write up my experiences on it.

This setup is powerful and yet very simple. The key features are:

2 Updates

3 Key Specs and Requirements

4 Design

The reporting framework will be integrated into CodeIgniter in the following way.

5 Implementation

I assume you have already installed CodeIgniter and setup your web application, if not then check the web for tutorials and help on getting started with CodeIgniter.

5.1 DOMPDF Plugin

  1. Download DOMPDF from http://dompdf.com. I am using version 0.6.0 beta 1 and so far I have experienced no problems despite it's beta status.
  2. Extract dompdf to your CodeIgniter application/plugins directory.
  3. I found I needed to execute the following command to prevent font errors.
    chmod a+x application/plugins/dompdf/lib/fonts
    
  4. Create a new plugin file: application/plugins/dompdf_pi.php with the following contents:
    <?php
    function dompdf_create($html, $filename, $path='', $stream=TRUE) {
      require_once("dompdf/dompdf_config.inc.php");
      spl_autoload_register('DOMPDF_autoload');
    
      $dompdf = new DOMPDF();
      $dompdf->load_html($html);
      $dompdf->render();
      if($stream) {
        $dompdf->stream($filename.".pdf");
      }
      else {
      $CI =& get_instance();
      $CI->load->helper('file');
      write_file($path . $filename."pdf", $dompdf->output());
      }
    }
    ?>
    
  5. If you want to default to A4 paper (I do) then edit application/plugins/dompdf/dompdf_config.inc.php changing this:
    define("DOMPDF_DEFAULT_PAPER_SIZE", "letter");
    

    to this:

    define("DOMPDF_DEFAULT_PAPER_SIZE", "a4");
    

That is all you need to do for the plugin.

5.2 Controller

Create a new reports controller and add this function:

function generate($name) {
  // extract any parameters from the rest of the URI
  $params = $this->uri->uri_to_assoc(4);

  // load report and generate pdf
  $this->load->plugin('dompdf');
  $html = $this->load->view('reports/dompdf/'.$name, array('params'=>$params), true);
  dompdf_create($html, $name);
}

The first parameter to the function is the report name which the function uses to lookup the appropriate view. The rest of the URI string can be used to pass parameters to your report i.e. a person id to look up.

5.3 Views (Reports)

Create a directory to hold your report views. I suggest application/views/reports.

Here is how I structure a simple report:

<!--query for data-->
<?php
  $sql = "
SELECT name, address, email 
FROM people 
WHERE id =".$params['person_id']."
";
  $person = $this->db->query($sql)->row();
  $CI =& get_instance();
  $CI->load->library('MyUtil');
  $extraData = $CI->myutil->getEnrolment($params['person_id']);
?>

<!--report styles-->
<style type='text/css'>
  #report_body {
    border-collapse: collapse;
    margin: 36pt;
  }
  .center {
    text-align: center;
  }
  table th, table td {
    font-size: 10pt;
    font-family: helvetica;
    padding: 0px;
  }
  #name {
    font-weight: bold;
    font-style: italic;
    font-size: 14pt;
  }
  #extraData {
    border-top: 1pt solid #000;
  }
</style>

<!--begin report-->
<table id='report_body' align='center' width='523pt'>
  <tr><td>Name:</td><td id='name'><?= $person->name ?></td></tr>
  <tr><td>Address:</td><td><?= $person->address ?></td></tr>
  <tr><td>Email:</td><td><?= $person->email ?></td></tr>
  <!--extra data-->
  <tr><td class='center' id='extraData'>
    <?= $extraData ?>
  </td></tr>
</table>

Of course you can go much more complex than this but check the dompdf website to see what is supported because currently not all CSS styling is supported.

5.4 Usage

Now to use the reports from within your application all you have to do is place a link something like this:

<a href='/reports/generate/contact/person_id/1234'>Get PDF</a>

Assuming you have a person in your database with the id of 1234 and a report named contact i.e. the report above.

6 Conclusion

I have had success using DOMPDF to generate reports in the application I am currently working on. It is a bit fiddly to write the reports and get them looking good because of the translation from CSS to print but is is the simplest way I have found so far of adding reasonably powerful reporting to your web-based application. The reason for this simplicity is the fact that generating and filling a report is just like displaying a view in CodeIgniter. You can use all the PHP functions you have created for your application to fill your reports as well and you have the power and flexibility of PHP in generating them.

I hope you have found this write-up useful.

7 References

The following sites may be helpful

Thanks to the people who wrote the CodeIgniter wiki page above which got me started on this.


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.