How to generate pdf in PHP ?


  How to export grid data’s as PDF using PHP (Codeigniter)?

                   
  Today I have a new article for you all here. In my last post you saw major difference between ASP & ASP.NET. Today I am going to show you how to export grid datas as PDF using php. There is lots of plug-ins available to generate PDF in PHP. Here I am going to show you two best PDF generator which I used for my professional field.  I am well versed with dom-pdf and m-pdf. These two PDF generators really great I found compare to other. So here I am going to show you  “ How to export grid data as PDF in PHP ? “  
All you have to do first is you have to download dom-pdf or m-pdf zip files. If you are wondering where can you get the zip files then here is the links to download dom-pdf and m-pdf.  These are the steps you need to follow to generate PDF using dompdf and mpdf.
        Step 1:
                After downloading the zip files just extract it.  You’ll get a folder (I renamed it “dompdf” / “mpdf”), and put the whole thing into your helper folder, ie: /system/helpers/dompdf
       Step 2:
              Create plug-in for the dompdf. I have created plug-in and named it dompdf_helper.php and write below lines of code inside the file.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function 
pdf_create($html$filename=''$stream=TRUE
{
    
require_once("dompdf/dompdf_config.inc.php");
    
    
$dompdf = new DOMPDF();
    
$dompdf->load_html($html);
    
$dompdf->render();
    if (
$stream{
        $dompdf
->stream($filename.".pdf");
    
else {
        
return $dompdf->output();
    
}
}
?> 
Or



<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function 
pdf_create($html$filename=''$stream=TRUE
{
   require_once("dompdf/dompdf_config.inc.php");    spl_autoload_register(“DOMPDF_autoload”);   
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream{
        $dompdf
->stream($filename.".pdf");
    
else {
        
return $dompdf->output();
    
}
}
?>
          For m-pdf extracted folder into your library folder.

Step 3 :
            That’s it you have done almost. Now to call your dompdf helper file use a function in your controller to generate PDF file. Here is the code you need to write,
  <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

  function exportPDF() {
     $this->load->helper(array('dompdf''file'));
     
// page info here, db calls, etc.     
     
$html $this->load->view('controller/viewfile'$datatrue);
     
pdf_create($html'filename');
     or
     
$data pdf_create($html''false);
     
write_file('name'$data);
     
//if you want to write it to disk and/or send it as an attachment   
   }
Or
    /* You can write your function like this also if you have separate view file to generate pdf */
function exportPDF() {
$year = $this->input->post(“currentyear”);
$monthname = $this->input->post(“month”);
$data[‘reportList’] = $this->pdfgenerator_model->getpdfDetails($id,$month,$year);
$dataView = $this->load-> view(‘pdf/report’,$data,true);
$this->load->helper(array('dompdf''file'));
$this->load->helper('file'));
      $pdfName = $monthname.”-”.$year.” 'Report'”;
      $pdfData = pdf_create($dataView,$pdfName);
      Monthnamewrite_file('My Monthly Report' ,$pdfData);
}    
?>
And If you want to use mpdf then it’s easy to generate. Here is the line of code to generate PDF using mpdf.
<?php
function generatePdf() {
$this->load->library(“mpdf51/mpdf”);
$data[‘reportList’] =$this->pdfgenerate_model ->getDetails();
$htmview = $this->load-> view(“pdfview”,$data,true);
$CI->mpdf = new mPDF('', 'A4', 0, '', 12, 12, 10, 10, 5, 5);
$this->mpdf->WriteHTML('<p>Hello There</p>',2);
$this->mpdf->Output(); 
}
?>
Step 4:
           That’s all you have done all steps. Click in export button to export to PDF and enjoy.

          This is how i generate PDF in PHP. Hope this post will help you a lot. Please do share and post your comments here. And If you have queries and if you want me to discuss then Please post your comments here. In my next post i will show you how to write your own helper file to generate PDF. So that you don't have to write much line of code, only you need to do is call helper and pass parameters to that. All the best. Have a good day.





Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions