Create your own helper file to generate PDF in PHP(Codeigniter)

           

 As of late, we have talked about "How to produce PDF utilizing PHP".  Today I am going to compose custom helper file to generate PDF. Did you ever thought of creating your own custom helper file to generate PDF in PHP(Codeigniter)?

Below custom helper script will generate a PDF file. You can export grid data into a PDF document.
I am going to create a single helper file to export any grid data into PDF. All you have to do is,  call my helper file and pass your data into the function, That’s all done. This script will generate PDF page.

Creating helper will help you to reuse the code and you don’t have to write much lines of codes. So let’s follow few steps as below.

 

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,

What is the Difference between ASP and ASP.NET ?



My past post was about "How to use memcache with PHP". As of late I got a request to write about "Key Differences Between Classic ASP And ASP.NET". In this post we will discuss on Difference between ASP vs ASP.NET. It is going to be very useful for your technical interview and for beginners in .NET. Here are the list of key differences between ASP and ASP.NET as below.

ASP:

i. ASP has limited OOP support. It is not supporting XML by default.

ii. Development and debugging tools unavailability. There are limited debugging tools available for ASP. Meaning that it is difficult to debug the code.

iii. In ASP we can use Visual Basic and Java for programming language.

iv. Error handling is very poor.

v. Classic ASP don't have high level programming structure. ASP pages are mix of HTML and Server side script.

vi. ASP page should have below code on the top page -


<%LANGUAGE="VBSCRIPT" CODEPAGE="960"%>

vii. It don't have built in validation control. Page validation is difficult for developers.

viii.In classic ASP it is required to restart the server in order to check latest updation on the page.

ASP.NET

i. ASP.NET is a full featured object oriented programming language.

ii. It has a full support of XML. It helps easy data exchange.

iii.There are many tools and compiler available. Microsoft Visual Studio makes your debugging job very easier.

iv. We can either use C# or VB.NET as a server side programming language.

v. ASP.NET provides three tire application architecture helps to keep your business logic and views separate. It helps on enhancing existing application without any difficulties.

vi. Error handling is very good.

vii.You should make a language directive on the page as below:


<%@Page Language="VB" CodePage="960"%> 
<%@QutputCache Duration="60" VaryByParam="none" %> 

viii. It is supporting state management.

ix. It has built in validation controls such as custom validator, range validator, regular expression, compare and require field validation control etc. It makes your job easier.

These are the key differences between classic ASP and ASP.NET. Hope this post will be helpful to you. Please don't forget to leave your comment below and share with friends. :)

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.