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:
The reporting framework will be integrated into CodeIgniter in the following way.
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.
chmod a+x application/plugins/dompdf/lib/fonts
<?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());
}
}
?>
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.
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.
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.
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.
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.
The following sites may be helpful
Thanks to the people who wrote the CodeIgniter wiki page above which got me started on this.
