Codeigniter . May 15, 2021 . By Ankit Pathak

How to generate and download PDF in codeigniter using mPDF ?

How to generate and download PDF in codeigniter using mPDF ?

Hi User, Thanks for visiting this blogĀ for your query. With the help of this article you will learn about how to generate PDF file in codeigniter with the help of mPDF Library.

Steps for configuring mPDF in codeigniter to generate and download pdf

1. Download and extract mPDF

Download mPDF from : http://www.mpdf1.com/mpdf/index.php and Extract it to your application/third_party/ folder of your CodeIgniter.

2. Create a new file in your application/libraries/ name it M_pdf.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 = "'c', 'A4-L'"){
$this->param =$param;
$this->pdf = new mPDF($this->param);
}
}

3. Create a file in controller and paste the below code

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class PDF extends CI_Controller {
public function generatePDF(){
// Name of your pdf file
$filename = time()."_filename.pdf";
// fetch data from the database
$data = $this->YourModel->modelMethod();
// pass data to view
$html = $this->load->view('pdfview', $data, true);
// load the library
$this->load->library('M_pdf');
$this->m_pdf->pdf->WriteHTML($html);
//download it D save F.
$this->m_pdf->pdf->Output("./assets/doc/".$filename, "F");
echo "PDF has been generated successfully!";
}
}
/* End of file pdf.php */
/* Location: ./application/controllers/pdf.php */

I hope this will help out to resolve your query. If you found any issue please share it in the comment box.

Web Hosting

  • Tag:
Chat on WhatsApp