Showing posts with label Codeigniter. Show all posts
Showing posts with label Codeigniter. Show all posts

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,

Which Framework to use Zend or Codeigniter ??


Introduction:


Hello! Performance is always matters a lot for modern applications where millions of users visit your website daily. I personally like to use MVC framework for web application development. It helps me to build faster, structured and modular application. I also believe unless building enterprise level development you don't require large scale fatty framework like Zend, Symfony etc. You may find many questions on the web like "Which is the best PHP Framework ? " etc. My personal opinion is nothing called "best", until you know to code well. Even using good framework you may code worst. In this article I will discuss why many people prefer CodeIgniter over Zend Framework.

I trust programming should be always clean, enjoyable, fun using latest tools, updated features etc. We use Framework to handle all basic task on ease. There are various reason why Codeigniter popular, also has disadvantage over other frameworks too. I will discuss below.

CodeIgniter Vs Zend

The main reason why I love CodeIgniter because of is its performance. CodeIgniter three times faster then the Zend framework. Also CodeIgniter is faster than other popular PHP MVC frameworks. Take a look at these Benchmark test done by Rasmus Lerdorf(creator of PHP programming), here , here. And also take a look at another benchmark test. So it’s clear that CI is faster than other Framework because of tiny library. Rasmus likewise told that he loved CodeIgniter on the grounds that it is quicker, lighter and the least like a structure. 

Another primary purpose for suggestion, it has low expectation to learn and adapt. Downloads come with a tremendous user guide, which is a great documentation like PHP Manual to get started with. I have been using CodeIgniter for last 2 years for web projects as a beginner level, from my personal experience, CodeIgniter has very easy, understandable structure compared to CakePHP or Zend Framework. If you’re a beginner and want to kick start your development structured way with PHP frameworks than I recommend you to go with CodeIgniter for understanding how MVC works. And jump to some other framework when you are comfortable with.

CodeIgniter coding conventions are flexible and simple. Ironically, CakePHP, Zend has very strict coding conventions. So you should be very careful about such cases while coding.

Still question in mind "Why not Zend Framework and why CodeIgniter?" Yes! Zend is PHP company and strong supporter. Ok, So question goes here is "What is so good about Zend Framework ?" Eventually! answer is Zend contains huge set of libraries and classes compared to any other framework. But slow performer. Ok! here another great news is that You can use Zend Framework’s classes in the CodeIgniter using Hooks available in CI. So you can even use any libraries from ZEND as a component in CI. Is it not great! yes.

Conclusion:

Although there are many features may catch your eye to use CodeIgniter but as web development is grown up faster, modern development requires strong object oriented programming for implementing better, robust, modular application. Of course a OOP MVC Framework for the better development, New Framework For Fun, Enjoyable development. In such cases CodeIgniter still lacking. CodeIgniter is still backdated compare to other MVC frameworks in PHP world. Still legacy code into the core where PHP introduced next generation version as PHP7.

Read: 5 Best things you should know about PHP7

Personally, I have learned many things from CodeIgniter as a beginner and towards building easy to use, hugely powerful, Stunningly Fast and enormously efficient modular HMVC Framework as "Cygnite PHP Framework- The New Cool Kid". Version 2.0 is under active development. You may have a look into it for better experience. Some benchmark of v1.3 here, and here

You can use the below comment form to let us know your thoughts and opinion. If you like this article please do not forget to share with your friends. To receive regular updates from us, you can either follow us on Facebook, Twitter, Google+ or you can subscribe to our monthly email newsletter.

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.