Generating PDF files using CodeIgniter
When generating reports in PDF format you suddenly have a lot more control over layout and design than you do with plain old HTML and CSS (although much progress is being made with print style sheets). You can create some really nice reports on the fly that your users can view, save for later or e-mail to their co-workers for review. In this post I will show you how I generate PDF reports using CodeIgniter.
There are a number of PHP libraries out there for generating PDF files (like FPDF, Panda and dompdf) but the best one is the R&OS pdf class. Trying other PDF libraries (some PHP 5 specific and some not) and none of them have been able to provide with the same control or ease of use that the R&OS class has which is used it for this tutorial.
There are 2 files that are necessary in order to use the R&OS library: class.ezpdf.php and class.pdf.php. Those two files have been placed in the application/library folder. R&OS also requires some font files in order to function and they have been placed at the root of the .zip file in a folder called fonts.
You do have to make a minor modification to the class.ezpdf.php file so that it will work properly within CI. First I renamed the file to cezpdf.php, which makes it easier to load using the CI loader. Then you have to modify the include statement on line 3 to:
include_once(APPPATH . ‘libraries/class.pdf.php’);
This will keep PHP from saying that it can’t find included file. Once those modifications are made the R&OS class is ready to use with CI.
In the archive, I have also made a controller called tutorial.php and a helper called pdf_helper.php both in their respective directories. With the background info. out of the way, lets get our hands dirty with a real simple example.
Hello World
function hello_world()
{
$this->load->library(‘cezpdf’);
$this->cezpdf->ezText(‘Hello World’, 12, array(‘justification’ => ‘center’));
$this->cezpdf->ezSetDy(-10);
$content = ‘The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.
Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.’;
$this->cezpdf->ezText($content, 10);
$this->cezpdf->ezStream();
}
Handling Tabular Data
When your dealing with business reports the odds are good that you will need to generate reports with tables to display tabular data. From my experience with other PDF libraries, this is not an easy task. But with the R&OS library it’s about as hard as creating an array.
{
$this->load->library(‘cezpdf’);
$db_data[] = array(‘name’ => ‘Jon Doe’, ‘phone’ => ’111-222-3333′, ‘email’ => ‘jdoe@someplace.com’);
$db_data[] = array(‘name’ => ‘Jane Doe’, ‘phone’ => ’222-333-4444′, ‘email’ => ‘jane.doe@something.com’);
$db_data[] = array(‘name’ => ‘Jon Smith’, ‘phone’ => ’333-444-5555′, ‘email’ => ‘jsmith@someplacepsecial.com’);
$col_names = array(
‘name’ => ‘Name’,
‘phone’ => ‘Phone Number’,
‘email’ => ‘E-mail Address’
);
$this->cezpdf->ezTable($table_data, $col_names, ‘Contact List’, array(‘width’=>550));
$this->cezpdf->ezStream();
Headers and Footers
Most reports in a corporate setting come with some kind of standard header and/or footer. They can include anything from the date/time generated, the user who generated them, to page numbers and the like. Headers and footers are handy to have, but unfortunately there isn’t a real good way to add them to printable reports generated in HTML and CSS. There’s one more reason to use the portable document format when creating printable reports. The process of adding headers and footers to a PDF using the R&OS library is the slightest bit complicated. There are a lot of lines of code just to accomplish it, that’s why I have created a helper file. Since the code is in a helper function, you just need load the helper and call the function whenever you need to add headers/footers to a PDF.
function prep_pdf($orientation = ‘portrait’)
{
$CI = & get_instance();
$CI->cezpdf->selectFont(base_url() . ‘/fonts’);
$all = $CI->cezpdf->openObject();
$CI->cezpdf->saveState();
$CI->cezpdf->setStrokeColor(0,0,0,1);
if($orientation == ‘portrait’) {
$CI->cezpdf->ezSetMargins(50,70,50,50);
$CI->cezpdf->ezStartPageNumbers(500,28,8,”,’{PAGENUM}’,1);
$CI->cezpdf->line(20,40,578,40);
$CI->cezpdf->addText(50,32,8,’Printed on ‘ . date(‘m/d/Y h:i:s a’));
$CI->cezpdf->addText(50,22,8,’CI PDF Tutorial – http://www.christophermonnat.com’);
}
else {
$CI->cezpdf->ezStartPageNumbers(750,28,8,”,’{PAGENUM}’,1);
$CI->cezpdf->line(20,40,800,40);
$CI->cezpdf->addText(50,32,8,’Printed on ‘.date(‘m/d/Y h:i:s a’));
$CI->cezpdf->addText(50,22,8,’CI PDF Tutorial – http://www.christophermonnat.com’);
}
$CI->cezpdf->restoreState();
$CI->cezpdf->closeObject();
$CI->cezpdf->addObject($all,’all’);
}
Enjoy !!!!
abc said,
February 26, 2010 at 12:08 pm
hi
how can i add value using database in pdf?
Md. Aminul Islam said,
March 19, 2010 at 2:38 pm
I hope, pray and wish you will be continue in touch of development. Regardless of technology you keep in touch of programming.
Wish you Best of Luck
Wayne said,
May 17, 2010 at 9:35 am
Hello
it would be nice if you could supply an example of your header function. I put it in a separate file and included it but can’t seem to get it to work.
Thanks
Wayne
adi kurniawan said,
October 8, 2010 at 8:34 am
can we export an existing table on html, or something we get from an div block on html?
thank you
uniquespirit said,
August 2, 2011 at 5:43 pm
it doesnt support UTF-8 charset . how can use unicode utf-8 in pdf and show correctly?
felixcheruiyot said,
August 9, 2011 at 7:09 pm
I like your tutorial very much.I find it quite helpful but I have a problem.
I have managed to generate a pdf file from mysql but the problem comes in the justification part when handling tabular data.
I’m handling data for currency data hence I prefer it justified right. Below is how I passed the justification option but It seems not to work.Please help.
$options=array(‘justification’=>’right’);
$this->cezpdf->ezTable($data['display_results'], $titlecolumn,’Payroll Report’,$options);