Kategori
PHP

Code Igniter : Export Data ke File PDF

Studi Kasus : Export Data Mysql Database ke dalam file PDF dalam Code Igniter.
Kebutuhan :
1. Webserver package, already installed.
2. Code Igniter Bundle.
3. Cezpdf library –> download here

Adakalanya, ketika kita membuat aplikasi, kita akan membutuhkan untuk mengexport data ke dalam file pdf (selain dari file excel), misalkan untuk membuat surat atau laporan tertentu. Dengan library cezpdf (karya Wayne Munro ), membuat laporan dalam bentuk pdf tidak akan terlalu sulit.

A. Mempersiapkan Database
1. Buat database dengan nama db_tutorial.
2. Buat tabel dengan nama tb_member, dengan struktur tabel dibawah ini,

3. Insert-kan beberapa sample data dalam tb_member, misal seperti data dibawah ini,

4. Okay, done with database.

B. Mempersiapkan file dan struktur direktori
1. Install Codeigniter, dengan mengekstrak bundle CI dan menempatkannya dalam document root anda.
2. Atur setting database (system-application-config-database.php) dengan setting database yang akan digunakan (db name : db_tutorial)
3. Atur config base url (system-application-config-config.php)
4. Letakkan file cezpdf.php dan class.pdf.php (hasil download Cezpdf library diatas) dalam folder system-application-libraries
5. Letakkan file pdf_helper.pdf dalam folder system-application-helper.
6. Ok πŸ˜€

C. Membuat model
1. Ketikkan script berikut ini,
[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. Simpan dengan nama member_model.php dalam folder system-application-models

D. Membuat Controller
1. Ketikkan script berikut ini,
[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. Simpan dengan nama member_model.php dalam folder system-application-models

E. Membuat View
1. Ketikkan script berikut ini,
[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. Simpan dengan nama member_model.php dalam folder system-application-models

F. Testing code
1. Buka http://localhost/ci_tutor/index.php/member_con. Anda akan melihat data yang ada dalam tb_member seperti gambar dibawah ini,

2. Klik link export to pdf untuk mengeksport data ke dalam file pdf.
3. Maka secara otomatis browser anda (atau download manager) akan men-download file pdf.
4. Buka file pdf, anda akan melihat data dari tb_member tertampil dalam file pdf tersebut πŸ™‚ Lihat gambar dibawah.

Happy coding πŸ˜€

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^^

19 replies on “Code Igniter : Export Data ke File PDF”

link to the resolution Mabosway sports betting interchange by registering – Are you looking for an looking for excitement and new experience in the
world of online gambling? You have come to the right place,
because here you can associate us, the best and most trusted gambling site agent in Indonesia!

We are privileged to be nimble to meet the expense of the best servants to members who are always loyal to
trust us and make us a gambling partner. Therefore, we have prepared various types of fun and
popular games such as Mabosway sports betting, casino, lottery,
and others.

Not single-handedly that, there are next many promos that we will have enough money and
of course it is every aimed at the benefit of the members.
all the facilities that are normal for you following you become a member here.
Here are some of our advantages exceeding new sites:

The mass and invalidation process is fast and accurate
Members’ personal data and identities are always safe and will not leak to supplementary parties
Mabosway login, one addict ID that can be used for various types of games
Customer staff who are always ready to help you 24/7
Unlimited Bonuses and Promos
The best support supported by years of experience has made Mabosway
always trusted by its members and earned the title as the best gambling site agent in Indonesia.

Not many online gambling sites in Indonesia have ascribed situation status and have licenses issued by relevant authorities.
In addition, the level of consumer service for online gambling sites can yet be
no question low because of the 100 affable online gambling sites, deserted
about 10-15 sites are dexterous to allow professional support and
meet standards.

Therefore, you cannot arbitrarily pick an online gambling site to be a playing partner.
There are many criteria and factors that must be considered to create an online gambling site a area to shelter and
save game deposits. However, it will acknowledge time and effort to locate a trusted site
as soon as this.

Hence, in this article we would considering to come up with the
money for important recommendations roughly trusted and certified online gambling sites in Indonesia.
By becoming a fanatic of this gambling site, it is sure that you will be release from doubt and one step closer to success.

Online gambling is no longer just a game but a issue that can take on you to a brighter future.

Like any extra situation in the world, every step you acknowledge will have alternative
risks and benefits. No human instinctive can always make a profit in every business, and no one can always avoid losing.
all that can be ended is to minimize losses and bump profits until finally the profits create
losses not felt.

ko muncul error seperti ini yah. .
Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\foxpro\system\libraries\cezpdf.php on line 695

setelah di perbaiki dia berhasil proses namun saat di pdf dia memberikan peringatan seperti ini….
” Format error:not a pdf a corrupted.” PERINGATAN
mohon pencerahan……..

maaf masih newbiew euy…..
proses berjalan lancar….Tp ko hasil pdf nya tidak menampilkan data…..
Mohon pencerahan…. πŸ˜›
terima kasih ini sngat membantu….
mudah2n Tuhan membalas,,,,,

kok muncul error seperti ini ?
“Cannot use object of type stdClass as array in C:\wamp\www\Intranet\application\libraries\cezpdf.php on line 695”

saat saya coba buka url ini

http://localhost/www/CodeIgniter/index.php/member_con

kug gini ya? salahnya apa ya?

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\htdocs\www\CodeIgniter\system\codeigniter\Common.php on line 130

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\htdocs\www\CodeIgniter\system\codeigniter\Common.php on line 136
A PHP Error was encountered

Severity: 8192

Message: Function set_magic_quotes_runtime() is deprecated

Filename: codeigniter/CodeIgniter.php

Line Number: 60

The URI you submitted has disallowed characters.

Makasih gan tutorilanya.. sangat bermanfaat

Untuk Mahfud yg masih ada error.
Coba copy-kan folder fonts ke root direktori CodeIgniter (folder fonts berada satu dg folder system),
dan pdf_helper.php ke system/aplication/helper

πŸ˜€

A PHP Error was encountered

Severity: Warning

Message: date() [function.date]: It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Krasnoyarsk’ for ‘7.0/no DST’ instead

Filename: libraries/class.pdf.php

Line Number: 689

A PHP Error was encountered

Severity: Warning

Message: date() [function.date]: It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Krasnoyarsk’ for ‘7.0/no DST’ instead

Filename: helpers/pdf_helper.php

Line Number: 24

A PHP Error was encountered

Severity: Notice

Message: Undefined index:

Filename: libraries/class.pdf.php

Line Number: 1631

denis => coba ini.

Ganti nama file class.ezpdf.php menjadi cezpdf.php agar lebih mudah.
Lalu modifikasi pad file cezpdf.php pada statement include di baris 3 menjadi seperti ini :

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

jangan lupa copy kan dulu file cezpdf.php dan class.pdf.php ke folder system/aplication/libraries dan pdf_helper.php ke system/aplication/helper…
selamat mencoba…

saya punya error seperti ini mas :

Fatal error: Class ‘Cpdf’ not found in C:\xampp\htdocs\lms\system\libraries\cezpdf.php on line 5

knp ya?

trims

keren tutorialnya mas…..tapi kalo mau menampilkan datanya per baris gmn mas?nanti dari field tsb bisa dipakai secara terpisah satu persatu..bisa ga kayak gt mas…mohon pencerahannya ya
terima kasih…

Tinggalkan Balasan ke tady 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.