Codeigniter how to create PDF Codeigniter how to create PDF codeigniter codeigniter

Codeigniter how to create PDF


Please click on this link it should work ..

http://www.php-guru.in/2013/html-to-pdf-conversion-in-codeigniter/

Or you can see below

There are number of PHP libraries on the web to convert HTML page to PDF file. They are easy to implement and deploy when you are working on any web application in core PHP. But when we try to integrate this libraries with any framework or template, then it becomes very tedious work if the framework which we are using does not have its own library to integrate it with any PDF library. The same situation came in front of me when there was one requirement to convert HTML page to PDF file and the framework I was using was codeigniter.

I searched on web and got number of PHP libraries to convert HTML page to PDF file. After lot of research and googling I decided to go with TCPDF PHP library to convert HTML page to PDF file for my requirement. I found TCPDf PHP library quite easy to integrate with codeigniter and stated working on it. After successfully completing my integration of codeigniter and TCPDF, I thought of sharing this script on web.

Now, let’s start with implimentation of the code.

Download the TCPDF library code, you can download it from TCPDF website http://www.tcpdf.org/.

Now create “tcpdf” folder in “application/helpers/” directory of your web application which is developed in codeigniter. Copy all TCPDF library files and paste it in “application/helpers/tcpdf/” directory. Update the configuration file “tcpdf_config.php” of TCPDF, which is located in “application/helpers/tcpdf/config” directory, do changes according to your applicatoin requirements. We can set logo, font, font size, with, height, header etc in the cofing file. Give read, write permissions to “cache” folder which is there in tcpdf folder. After defining your directory structure, updating configuration file and assigning permissions, here starts your actual coding part.

Create one PHP helper file in “application/helpers/” directory of codeigniter, say “pdf_helper.php”, then copy below given code and paste it in helper file

Helper: application/helpers/pdf_helper.php

function tcpdf(){    require_once('tcpdf/config/lang/eng.php');    require_once('tcpdf/tcpdf.php');}

Then in controller file call the above helper, suppose our controller file is “createpdf.php” and it has method as pdf(), so the method pdf() will load the “pdf_helper” helper and will also have any other code.

Controller: application/controllers/createpdf.php

function pdf(){    $this->load->helper('pdf_helper');    /*        ---- ---- ---- ----        your code here        ---- ---- ---- ----    */    $this->load->view('pdfreport', $data);}

Now create one view file, say “pdfreport.php” in “application/views/” directory, which is also loaded in pdf() method in controller. So in view file we can directly call the tcpdf() function which we have defined in “pdf_helper” helper, which will load all required TCPDF classes, functions, variable etc. Then we can directly use the TCPDF example codes as it is in our current controller or view. Now in out current view “pdfreport” copy the given code below:

View: application/views/pdfreport.php

tcpdf();$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$obj_pdf->SetCreator(PDF_CREATOR);$title = "PDF Report";$obj_pdf->SetTitle($title);$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $title, PDF_HEADER_STRING);$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));$obj_pdf->SetDefaultMonospacedFont('helvetica');$obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);$obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);$obj_pdf->SetFont('helvetica', '', 9);$obj_pdf->setFontSubsetting(false);$obj_pdf->AddPage();ob_start();    // we can have any view part here like HTML, PHP etc    $content = ob_get_contents();ob_end_clean();$obj_pdf->writeHTML($content, true, false, true, false, '');$obj_pdf->Output('output.pdf', 'I');

Thus our HTML page will be converted to PDF using TCPDF in CodeIgniter. We can also embed images,css,modifications in PDF file by using TCPDF library.


TCPDF is PHP class for generating pdf documents.Here we will learn TCPDF integration with CodeIgniter.we will use following step for TCPDF integration with CodeIgniter.

Step 1

To Download TCPDF Click Here.

Step 2

Unzip the above download inside application/libraries/tcpdf.

Step 3

Create a new file inside application/libraries/Pdf.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';class Pdf extends TCPDF{ function __construct() { parent::__construct(); }}/*Author:Tutsway.com *//* End of file Pdf.php *//* Location: ./application/libraries/Pdf.php */

Step 4

Create Controller file inside application/controllers/pdfexample.php.

 <?php    class pdfexample extends CI_Controller{     function __construct()    { parent::__construct(); } function index() {    $this->load->library('Pdf');    $pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);    $pdf->SetTitle('Pdf Example');    $pdf->SetHeaderMargin(30);    $pdf->SetTopMargin(20);    $pdf->setFooterMargin(20);    $pdf->SetAutoPageBreak(true);    $pdf->SetAuthor('Author');    $pdf->SetDisplayMode('real', 'default');    $pdf->Write(5, 'CodeIgniter TCPDF Integration');    $pdf->Output('pdfexample.pdf', 'I'); }    }    ?>

It is working for me. I have taken reference from http://www.tutsway.com/codeignitertcpdf.php