Wednesday, 23 November 2011

Creating PDF files with Codeigniter

Generating PDF files is required for saving and printing the documents like invoices or something.

The task is easy with free pdf generation libraries like dompdf, mpdf, fpdf etc.
Below is the code for generating pdf from html using mpdf library.
First extract mpdf files in codeigniter /library/mpdf53.

private function _gen_pdf($html,$paper='A4')
    {
        $this->load->library('mpdf53/mpdf');               
        $mpdf=new mPDF('utf-8',$paper);
        $mpdf->WriteHTML($html);
        $mpdf->Output();
    } 



To use the code below to simply redirect the output to the function as:


public function doprint($pdf=false)
    {
        $this->load->library('parser');
        $data = $this->data->getdata();       
        $output = $this->parser->parse('form',$data,true);   
        if ($pdf=='print')
            $this->_gen_pdf($output);
        else
            $this->output->set_output($output);               
    }



This will generate the pdf file within the browser.

Wednesday, 5 October 2011

Recommended libraries with Codeigniter.

Hi,
Here are few libraries which I use and recommend with Codeigniter.
Ion Authentication Library Very comfortable to use Authentication Library.
Grocery CRUD Good CRUD with great features.
Dynamic Menu Generated drop-down or other navigation menu through database.
(though I have done some modification in the library to be used with Free CSS Drop-Down Menu which I will be posting later.
Firephp for debugging, logging etc.


Friday, 16 September 2011

Extending Table Library in Codeigniter

Codeigniter has a great Table library to display data in html table format. Adding a Serial No is sometime required to a table, but Row num is not easily provided by MySQL hence while using Table library this function is added to do the job.

Here goes the code:
 /**  
    * Set the serial no  
    *  
    * Add serial no to left of table  
    *  
    * @access  public  
    * @param  mixed  
    * @return  mixed  
    */  
   function add_sr_no(&$arr)  
   {  if (!is_array($arr)) return $arr;  
     $i=1;  
     foreach ($arr as &$values) {        
       if (!is_array($values))  
         break;  
       else  
         array_unshift($values, $i++);  
     }      
     return $arr;  
   }  
Just add these lines to table.php in library.
Usage:
$rows = $query->result_array();
$this->table->add_sr_no($rows);
Don't forget to add first column 'Sr No' in set_heading.

Hope this helps.


Views are invited.