Kategori
PHP

Code Igniter : Export Data into PDF File

Case Study : Data Export Mysql Database into a PDF file in Code Igniter.
Requirements:
1. Webserver package, already installed.
2. Code Igniter Bundle.
3. Cezpdf library –> download here

Sometimes, when we make an application, we will need to exported data into a pdf file (apart from the excel file), for example to create letters or reports. With a library cezpdf (by Wayne Munro), make a report as a pdf not be too difficult.

A. Preparing Database
1. Create a database with the name db_tutorial.
2. Create a table named tb_member, with the structure of the table below,

3. Insert a few sample data in tb_member, such as the data below,

4. Okay, done with the database.

B. Prepare files and directory structure
1. Install CodeIgniter, by extracting the bundle CI and place it in your document root.
2. Set the database settings (system-config-application-database.php) by setting the database to be used (db name: db_tutorial)
3. Set the url base config (system-config-application-config.php)
4. Put the files cezpdf.php and class.pdf.php (the downloaded library Cezpdf above) in a folder system-application-libraries
5. Put the files in the folder pdf_helper.pdf-helper-application system.
6. Ok: D

C. Make a model
1. Type the following script,
[sourcecode language=”php”]
<?php
class Member_model extends Model {
function Member_Model() {
parent::Model();
}
function alldata()
{
$this->db->select(‘*’);
$this->db->from(‘tb_member’);
$this->db->order_by(‘no’,’ASC’);
$getData = $this->db->get();
if($getData->num_rows() > 0)
return $getData->result_array();
else return null;
}
}
?>
[/sourcecode]
2. Save with the name of the folder system member_model.php-models-application

D. Make Controller
1. Type the following script,
[sourcecode language=”php”]
<?php
class Member_con extends Controller {
public function __construct() {
parent::__construct();
$this->load->model(‘member_model’);
$this->load->helper(‘url’);
}
public function index() {
$data[‘member’] = $this->member_model->alldata();
$this->load->view(‘member_view’, $data);
}

function topdf () {
$this->load->library(‘cezpdf’);
$this->load->helper(‘pdf’);
prep_pdf();
$data[‘member’]= $this->member_model->alldata();
$titlecolumn = array(
‘no’ => ‘no’,
‘name’ => ‘name’,
‘address’ => ‘address’
);
$this->cezpdf->ezTable($data[‘member’], $titlecolumn,’Member Data’);
$this->cezpdf->ezStream();
}
}
?>
[/sourcecode]
2. Save with the name of the folder system member_model.php-models-application

E. Create View
1. Type the following script,
[sourcecode language=”php”]
<h4>Member Data</h4>
<?php if(count($member) > 0) { ?>
<table border="1">
<tr>
<th>No</th>
<th>Name</th>
<th>Address</th>
</tr>
<?php
foreach($member as $rows) {
echo "<tr>";
echo "
<td>". $rows[‘no’]."</td>
<td>". $rows[‘name’] ."</td>
<td>". $rows[‘address’] ."</td>
"; } ?>
</table>
<?php } ?>
<br> <br>
<a href='<?php echo base_url(); ?>index.php/member_con/topdf’><span

style=’color:green;’>Export to Pdf</span></a>
[/sourcecode]
2. Save with the name of the folder system member_model.php-models-application

F. Testing code
1. Open http://localhost/ci_tutor/index.php/member_con. You will see the data that exists in tb_member like the picture below,

2. Click the link export to pdf to export data into pdf file.
3. So automatically your browser (or download manager) will download the pdf file.
4. Open the pdf files, you will see data from tb_member displayed in pdf file:) See the picture below.

Happy coding: D

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

8 replies on “Code Igniter : Export Data into PDF File”

WordPress theems for Tamil 5. Fixing WordPress theems to display Tamil properly in Firefox 6. Fixing jilebi font or ???? issue for self-hosted blogs in Tamil for WordPress 2.0+ 7. WordPress plugin for Thamizmanam toolbar Tags: tamil in wordpress,

please help me, i have error.. Cannot use object of type stdClass as array in D:\xampp\htdocs\kesetaraan089_UNPAS\system\libraries\cezpdf.php on line 695

(idiot) (idiot) (idiot) (idiot) (idiot)
i can’t use it in Codeigniter 2.. can i ask why?
(smileydance) (smileydance) (smileydance)

Tinggalkan Balasan ke Sablon kaos jogja Batalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.