Easily integrate mPDF into CodeIgniter Framework

Got the tip from here: http://davidsimpson.me/2013/05/19/using-mpdf-with-codeigniter/
On this previous post, I tell you on how to easily integrate ezPDF into CodeIgniter but the problem is: the PDF generation is not template-based. So, you have to write a lot of codes just to generate simple PDF output.
By integrating mPDF into your CodeIgniter-based site, you can create a PDF output via templating system just as simple as loading a view and pass through some variables into it. mPDF also support CSS, so, you can style your HTML template as cool as you wish.
Here’s the steps-by-steps guide:

  • Download mPDF from http://www.mpdf1.com/mpdf/index.php?page=Download . Remember to download the “Full installation” first (a bit big) and then you can download the patch/upgrade (a little smaller size)
  • Extract it to your application/third_party/ folder of your CodeIgniter. Your third_party folder would be like this:

    mPDF in third_party folder

    mPDF in third_party folder

  • Create a new file in your application/libraries/ name it m_pdf.php and these are the contents:

  • Your library folder would be like this:
    mPDF in the library folder

    mPDF in the libraries folder

  • And it's done!

How to use it?
Create a CodeIgniter "view file", let's named it pdf_output.php:

load->view('header'); //let's assume that we already have 'header' view file
Hello, this just a simple HTML template.
I'm a variable passed from the controller, my content is
load->view('footer'); //let's assume that we already have 'footer' view file

Now, inside the controller, you can do this:

//this data will be passed on to the view
$data['the_content']='mPDF and CodeIgniter are cool!';

//load the view, pass the variable and do not show it but "save" the output into $html variable
$html=$this->load->view('pdf_output', $data, true); 

//this the the PDF filename that user will get to download
$pdfFilePath = "the_pdf_output.pdf";

//load mPDF library
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->m_pdf->load();
//generate the PDF!
//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output($pdfFilePath, "D");
Leave a comment ?


  1. previously i was using mpdf, it turns out very slow to convert a lot of pdf, i ended up using wkhtmltopdf instead , it’s very fast.. still the best free html to pdf solution

    • Does wkhtmltopdf working on shared hosting? Never tried it on shared hosting before.
      Soon I’ll release my new website that utilize wkhtmltopdf(and wkhtmltoimage). 🙂

      • I use shared hosting webfaction.com, i was able to run it perfectly. i never tried on cpanel based shared hosting though. what is your new website ? vrank.org ?

  2. Just wondering what the advertising network are you using on ytconv.net that shows “pl
    ay download button” ads

  3. php: Matrix operation library for CodeIgniter - pingback on 2014/09/08 at 13:59
  4. how to work it with css file ?>

  5. it works for me.thank u.can u tell me how can insert sql query in to the pdf.please

  6. hi, am getting error like this, what to do ?
    An Error Was Encountered

    Unable to load the requested class: M_pdf

  7. how can i print bangla font correctly using mpdf??

  8. //$pdf->Output($pdfPath, ‘D’); this line not works

  9. Aji Bhaskar

    Database values were not printed, showing errors. How can i solve this problem

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Trackbacks and Pingbacks: