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

Leave a Reply

Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments

  1. file will be store on server but not download in chrome browser when using D in place of F

    Reply
  2. can u tell how to save as pdf?

    Reply