Step 1:
Download files from dom-pdf and m-pdf and extracts it into your helper folder, ie: /system/helpers/dompdf (or mpdf ).
Step 2:
Now create your own helper and name it pdfexport_helper.php and paste the below lines of code. If you are using mpdf then use below code.
/*
* Subject : Export pdf using mpdf
* Author : Sanjoy Dey
* @Created Date : 10-02-2012
* Version : CodeIgniter_2.0.3
*
*/
if ( ! function_exists('exportMeAsMPDF')) {
function exportAsMPdf($htmView, $fileName)
{
$CI =& get_instance();
$CI->load->library('mpdf51/mpdf');
// $CI->mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
$CI->mpdf->AliasNbPages('[pagetotal]');
$CI->mpdf->SetHTMLHeader('{PAGENO}/{nb}', '1',true);
$CI->mpdf->SetDisplayMode('fullpage');
$CI->mpdf->pagenumPrefix = 'Page number ';
$CI->mpdf->pagenumSuffix = ' - ';
$CII->mpdf->nbpgPrefix = ' out of ';
$CI->mpdf->nbpgSuffix = ' pages';
$CI->mpdf = new mPDF('', 'A4', 0, '', 12, 12, 10, 10, 5, 5);
$style = base_url().'/source/template/css/stylesheet.css';
$stylesheet = file_get_contents( $style);
$CI->mpdf->WriteHTML($stylesheet,1);
$CI->mpdf->WriteHTML($htmView,2);
$CI->mpdf->Output('mpdf.pdf','I');
}
}
If you are using dompdf then use below code.
/*
* Subject : Export pdf using dompdf
* Author : Sanjay
* Version : CodeIgniter_2.0.3
*/
if ( ! function_exists('exportMeAsDOMPDF')) {
function exportAsDomPdf($htmView, $fileName)
{
$CI =& get_instance();
$CI->load->helper(array('dompdf', 'file'));
$CI->load->helper('file');
$pdfName = $fileName;
$pdfData = pdf_create($htmView, $pdfName);
write_file('Progress Repost', $pdfData);
}
}
?>
Step 3:
Open your controller page and inside your function write below code.
#Export Function goes here#
/*This Function is used for Exporting Pdf
* Author : Sanjay
*/
$this->load->helper(‘pdfexport_helper.php’);
$urlId = $this->uri->segment('3');
if($urlId == "export") {
$data['pageTitle'] = "Annual Report";
$data['htmView'] = $this->load->view('annualreport_view',$data,TRUE);
$templateView = $this->load->view('../template_export',$data,TRUE);
exportAsMPdf($templateView,$data['filename']);
OR
exportAsDomPdf($htmView,$fileName)
}
/* Export Pdf END here */
Step 4:
That’s all you have done all steps. Click in export button to export to PDF and enjoy.
Hope this post will help you a lot to reuse your code and creating your own helper file to generate PDF in PHP(Codeigniter).
Post your queries if you have and share. Keep visiting for new posts here. All the best. Have a good day.
You also can join my group for latest technology updates in below link,