mPDF is "a PHP class to generate PDF files from HTML with Unicode/UTF-8 and CJK support".

Here's a short guide to using mPDF with CodeIgniter. I'm currently using CodeIgniter 2.1.3, but this should work with previous versions back to v1.6.

Download and extract mPDF

Download mPDF and extract it to

/application/third_party/mpdf
so that you have a file structure like so:

Create the CodeIgniter library for mPDF

This is a simple wrapper for mPDF. Create /application/library/pdf.php:


<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class pdf {

function pdf()
{
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}

function load($param=NULL)
{
include_once APPPATH.'/third_party/mpdf/mpdf.php';

if ($params == NULL)
{
$param = '"en-GB-x","A4","","",10,10,10,10,6,3';
}

return new mPDF($param);
}
}

Call the library in your code

Add some code in your controller e.g.


...
// As PDF creation takes a bit of memory, we're saving the created file in /downloads/reports/
$pdfFilePath = FCPATH."/downloads/reports/$filename.pdf";
$data['page_title'] = 'Hello world'; // pass data to the view

if (file_exists($pdfFilePath) == FALSE)
{
ini_set('memory_limit','32M'); // boost the memory limit if it's low ;)
$html = $this->load->view('pdf_report', $data, true); // render the view into HTML

$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure ;)
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F'); // save to file because we can
}

redirect("/downloads/reports/$filename.pdf");
...

That's it. Very quick and simple. The mPDF files are untouched in a self contained directory, but called by the custom

/application/library/pdf.php
script.

FWIW: Here's an example file created using mPDF.

Update:
A major benefit of this approach over some of the others I've seen is that this wrapper does not copy any code from the mPDF library onto your file structure elsewhere. If you wish to upgrade the mPDF library, just replace the files below

/application/third_party/mpdf

The wrapper should take care of the rest with minimal changes.

Further reading

If you need any help with mPDF rather than just fitting it into CodeIgniter, these are the best places to visit: