Print pdf with mPdf Librarry Codeigniter

mPDF is “a PHP class to generate PDF files from HTML with Unicode/UTF-8 support”. Here is a short guide to using mPDF with CodeIgniter.
I tested with CodeIgniter 3.1.4 version.

Controller/PrintToPdf.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

#######################################
#
#        Author: Guliver
#        E-mail: guliver@gulivera.net
#        Date: 24.03.2017
#
#######################################
class PrintToPdf extends CI_Controller
{
	public function printpdf()
    {
        ini_set('memory_limit', '256M');
		
		//// Inlucde mpdf Library////
        $this->load->library('pdf');
        $pdf = $this->pdf->load();


        $data['pdf'] = "ჩემი პირველი PDF";
		
		// Inlcude View File////
        $html = $this->load->view('printpdfInvoice', $data, true);
		
		///Convert UTF-8 to view Georgia Font///
        $html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');
		
        // render the view into HTML
        $pdf->WriteHTML($html);
		
        // write the HTML into the PDF
        $output = 'Invoice' . date('Y_m_d_H_i_s') . '_.pdf';
        $pdf->Output("$output", 'I');
    }
	

}

?>

library/Pdf.php

<?php
#######################################
#
#        Author: Guliver
#        E-mail: guliver@gulivera.net
#        Date: 24.03.2017
#
#######################################
class Pdf {

    function Pdf() {
        $CI = & get_instance();
        log_message('Debug', 'mPDF class is loaded.');
    }

    function load($param = NULL) {
        require_once APPPATH .'third_party/mpdf/mpdf.php';

        if ($param == NULL) {
            $param = '"en-GB-x","A4","","",10,10,10,10,6,3';
        }
        return new mPDF($param);
    }

}


views/printpdfInvoice.php

გამარჯობა, 
<?php echo $pdf;?>

Download Full

2 thoughts on “Print pdf with mPdf Librarry Codeigniter

Leave a Reply

Your email address will not be published. Required fields are marked *

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