Comments

Tuesday, February 28, 2012

Generating PDF files from Database using CodeIgniter

Posted by on Tuesday, February 28, 2012 Read our previous post
Before we start here is a brief information about CodeIgniter. CodeIgniter is an open source web application framework built for PHP coders who want to develop a full-feature web application. It has libraries which help us to perform many tasks very easily. It works on the MVC Pattern.


As a programmer I find PDF files very helpful to me when generating reports and getting them printed.

We will be using R&OS pdf class. I find this to be the best one because all others libraries I came across didn't offer me a good control over the making of the file and also the process of making i.e the code required for this library is bit tricky but it helped me improve my coding.

Lets Start :


Before we start, make sure you have downloaded the zip file and if not download it from here. After downloading it unzip/ extract the files and folders. 

We will be loading data for our pdf file from our mysql database. So make sure you have configured the your database. To make your database work goto the config folder and in that open the database.php and fill in the details about your DB.

After extraction is done, you will find two files namely class.ezpdf.php/cezpdf.php and class.pdf.php. Now put these two .php files inside application/libraries. To make these work within CI you will have to a modification in the cezpdf.php/class.ezpdf.php. The modification is to be done in the include statement : 


include_once(APPPATH . 'libraries/class.pdf.php');

Now go to your controller folder and there make a new file name generate.php and pdf_helper.php.

pdf_helper.php :


<?php

function prep_pdf($orientation = 'portrait')
{
    $CI = & get_instance();
    
    $CI->cezpdf->selectFont(base_url() . '/fonts');    
    
    $all = $CI->cezpdf->openObject();
    $CI->cezpdf->saveState();
    $CI->cezpdf->setStrokeColor(0,0,0,1);
    if($orientation == 'portrait') {
        $CI->cezpdf->ezSetMargins(50,70,50,50);
        $CI->cezpdf->ezStartPageNumbers(500,28,8,'','{PAGENUM}',1);
        $CI->cezpdf->line(20,40,578,40);
        $CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));
        $CI->cezpdf->addText(50,22,8,'PDF Tutorial - www.code2learn.com');
    }
    else {
        $CI->cezpdf->ezStartPageNumbers(750,28,8,'','{PAGENUM}',1);
        $CI->cezpdf->line(20,40,800,40);
        $CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));
        $CI->cezpdf->addText(50,22,8,'PDF Tutorial - www.code2learn.com');
    }
    $CI->cezpdf->restoreState();
    $CI->cezpdf->closeObject();
    $CI->cezpdf->addObject($all,'all');
}

?>

generate.php :


<?php

class Generate extends CI_Controller
{

    function Generate()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
    }


    function create()
    {

                $this->load->library('cezpdf');

        $this->cezpdf->ezText('PDF REPORT OF LOGIN TABLE', 12, array('justification' => 'center'));
        $this->cezpdf->ezSetDy(-10);
                $i=1;
                $content="";

                $fname="";
                $query = $this->db->query('SELECT * FROM table_name');
                $num = $query->num_fields();
                $farr=array();

                while($i <= $num){
                    $test = $i;
                    $value = $this->input->post($test);

                    if($value != ''){
                            $fname= $fname." ".$value;
                            array_push($farr, $value);

                        }
                     $i++;
                }

                $fname = trim($fname);

                $fname=str_replace(' ', ',', $fname);
                $this->db->select($fname);
                $query = $this->db->get('table_name');
                $result = $query->result();

                foreach ($farr as $j)
                {

                    $content= strtoupper($j)."\n\n";
                    foreach($result as $res){
                       $content = $content.$res->$j."\n";
                    }

                      $this->cezpdf->ezText($content, 10);

                       $this->cezpdf->ezStream();
                 }

    }


In the above, first thing we do is load the R&OS library for use. Next we use the ezText() function to create a title for our document. This function takes the text it will display as the first argument, the size of that text and an optional array of additional configuration options. 

After the whites pace we put the rest of the content for the document in a variable called $content and add it to our document using the ezText() function again. Finally, we create our document using the ezStream() function which actually creates the document and sends it to the users which prompts them to view/download the generated PDF document.

© 2010 Code 2 Learn