Hello friends , I have stated using codeigniter framework for php. I would recommended codeigniter for all PHP developers.
Recently i had a task to generate PDF file in my codeigniter project ,there are many libraries like FPDF, Panda, DOM-pdf, R&OS to generate PDF documents with PHP.
I found a method to integrate R&OS pdf class file with codeigniter. I am sharing you the same.
Step 1
Download latest version of
Codeigniter framework.
Step 2
Download the class files for pdf integration from
here. The rar folder contains 2 folders.
1. library class files
2. fonts
Extract files under
class files folder to application/libraries/
Extract
fonts directory to root folder of your CI application.
Step 3
Now into final stage . Create a controller named
home.php and paste the following code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->hello_world();
}
function hello_world()
{
$this->load->library('cezpdf');
$this->cezpdf->ezText('Hello World', 12, array('justification' => 'center'));
$this->cezpdf->ezSetDy(-10);
$content = 'The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.
Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.';
$this->cezpdf->ezText($content, 10);
$this->cezpdf->ezStream();
}
function tables()
{
$this->load->library('cezpdf');
$db_data[] = array('name' => 'Jon Doe', 'phone' => '111-222-3333', 'email' => 'jdoe@someplace.com');
$db_data[] = array('name' => 'Jane Doe', 'phone' => '222-333-4444', 'email' => 'jane.doe@something.com');
$db_data[] = array('name' => 'Jon Smith', 'phone' => '333-444-5555', 'email' => 'jsmith@someplacepsecial.com');
$col_names = array(
'name' => 'Name',
'phone' => 'Phone Number',
'email' => 'E-mail Address'
);
$this->cezpdf->ezTable($db_data, $col_names, 'Contact List', array('width'=>550));
$this->cezpdf->ezStream();
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/home.php */
You are done .
Execute the code :
http://localhost/pdf/index.php/home/index – for basic pdf creation.
http://localhost/pdf/index.php/home/tables – for table creation.
Note: In Order to write the output as a
.pdf file just change
$this->cezpdf->ezStream(); to
$this->cezpdf->ezOutput();
Complete Code :
create a folder named pdf in root directory of codeigniter project and replace the tables function in the above controller
[
home.php ] with the following.
function tables()
{
$this->load->helper('path');
$this->load->library('cezpdf');
$this->cezpdf->ezText('Hello World', 12, array('justification' => 'center'));
$this->cezpdf->ezSetDy(-10);
$db_data[] = array('name' => 'Jon Doe', 'phone' => '111-222-3333', 'email' => 'jdoe@someplace.com');
$db_data[] = array('name' => 'Jane Doe', 'phone' => '222-333-4444', 'email' => 'jane.doe@something.com');
$db_data[] = array('name' => 'Jon Smith', 'phone' => '333-444-5555', 'email' => 'jsmith@someplacepsecial.com');
$col_names = array(
'name' => 'Name',
'phone' => 'Phone Number',
'email' => 'E-mail Address'
);
$this->cezpdf->ezTable($db_data, $col_names, 'Contact List', array('width'=>550));
$directory = './pdf/';
set_realpath($directory);
$file = $directory.date('d-m-Y h:i:s').'.pdf';
$pdfcode = $this->cezpdf->ezOutput();
$fp = fopen($file,'wb');
fwrite($fp,$pdfcode);
fclose($fp);
}
You are done !