Generate PDF dalam CodeIgniter

Written by Azrul Abdul Latif


1. Download mPDF library di sini: http://www.mpdf1.com/mpdf/index.php

2. Extract mPDF library dalam: /application/third_party/mpdf

3. Copy library mPDF dalam /application/library/pdf.php. Sila download library tersebut di sini:

Generate PDF using CodeIgniter

4. Create view

4. Call the library in your code

// 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");

6. Demo:

Reference:

1. Using mPDF with CodeIgniter