Wednesday 23 November 2011

Creating PDF files with Codeigniter

Generating PDF files is required for saving and printing the documents like invoices or something.

The task is easy with free pdf generation libraries like dompdf, mpdf, fpdf etc.
Below is the code for generating pdf from html using mpdf library.
First extract mpdf files in codeigniter /library/mpdf53.

private function _gen_pdf($html,$paper='A4')
    {
        $this->load->library('mpdf53/mpdf');               
        $mpdf=new mPDF('utf-8',$paper);
        $mpdf->WriteHTML($html);
        $mpdf->Output();
    } 



To use the code below to simply redirect the output to the function as:


public function doprint($pdf=false)
    {
        $this->load->library('parser');
        $data = $this->data->getdata();       
        $output = $this->parser->parse('form',$data,true);   
        if ($pdf=='print')
            $this->_gen_pdf($output);
        else
            $this->output->set_output($output);               
    }



This will generate the pdf file within the browser.

7 comments:

  1. We can generate PDF in CodeIgniter using TCPDF library also, please check the URL
    http://www.php-guru.in/2013/html-to-pdf-conversion-in-codeigniter/

    ReplyDelete
  2. Are the above two blocks of code to be place in the controllers or what?

    ReplyDelete
  3. Where should the codes be place at? Controller? Library? or what? Thank you

    ReplyDelete
  4. Are you searching for a good JPG to PDF converter ? If yes, then you must use this JPG to PDF converter from PDFOnline.com.

    ReplyDelete
  5. So you want to be a work from home typist? It's a great job, and you can make great money typing online as long as you know where to start. If you can type, have a computer and internet access then there's no reason why you can't be very successful. See more png to html

    ReplyDelete