Generate and Download PDF file in Codeigniter - Mostlikers

11 April, 2019

Generate and Download PDF file in Codeigniter

After a long time, I am writing a new article. Thanks for everyone's support. Today we are going to see How to create and download PDF files by using CodeIgniter mPDF library. mPDF is a customized third-party library, using this simple library function we will convert our HTML template code to PDF. Follow the below step by step process.

Generate and Download PDF in Codeigniter using mPDF

Live Demo            Download Script 

Step 1: Download the latest version of mPDF library files. Load the files in your third-party (new directory)  or library folder.

Path: application\third_party\

Step 2: Create a new file in your library (mpdf.php) and paste the below code.
path: application\ibraries\

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 include_once APPPATH.'/third_party/mpdf/mpdf.php';
class M_pdf {
    public $param;
    public $pdf;
    public function __construct($param = '"en-GB-x","A4","","",10,10,10,10,6,3')
        $this->param =$param;
        $this->pdf = new mPDF($this->param);

Step 3: Create a new controller or use your existing controller to load the below function,

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
 public function index()
  $html=$this->load->view('mypdfLayout',"", true); // html file
  $pdfFilename = "myfilename.pdf";
  $this->m_pdf->pdf->Output($pdfFilename, "D");  

Step 4: Create an HTML template and load in your view folder.