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,
<?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;
}
}
?>
2. Save with the name of the folder system member_model.php-models-application
D. Make Controller
1. Type the following script,
<?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();
}
}
?>
2. Save with the name of the folder system member_model.php-models-application
E. Create View
1. Type the following script,
<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>
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
Others that might interesting too:
Copyright
All scripts and techniques demonstrated on itx.web.id can be used in whatever manner you wish without attribution. You cannot copy whole tutorials, either in English or translated to another language.
This post is also available in: Indonesian
English
Indonesia
(idiot) (idiot) (idiot) (idiot) (idiot)
i can’t use it in Codeigniter 2.. can i ask why?
(smileydance) (smileydance) (smileydance)
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
not working
am getting “undefined index error”