Anda di halaman 1dari 5

Membuat Laporan(Report) dg FPDF,

Html2FPDF & Html2PDF


Apa itu fpdf, html2fpdf& html2pdf?
Secara umum fpdf, html2fpdf, dan html2pdf adalah library dari PHP untuk membuat file
PDF. hal ini sangat berguna jika kita membangun sistem reporting. PDF(Portable Document
File) merupakan file yang bisa di baca di semua OS. jadi inilah alasannya kenapa memakai
PDF untuk reporting.
Gak usah banyak cas-cis-cus langsung aja saya berikan contoh dari ke-3 library di atas.
1. FPDF
001 <?php
002 include'fpdf.php';
003 mysql_connect('localhost','root','');
004 mysql_select_db('dbrutan');
005 $bulan= $_POST['bln'];
006 $tahun= $_POST['thn'];
$sql= "SELECT * FROM napi a JOIN dtpenghuni b ON a.id_penghuni =
b.id_penghuni WHERE status = 'Bebas' AND EXTRACT(MONTH FROM tgl_bebas)=
007
'$bulan' AND EXTRACT(YEAR FROM tgl_bebas)= '$tahun' ORDER BY id_napi
DESC";
008
009 $qry=mysql_query($sql) ordie(mysql_error());
010 define('FPDF_FONTPATH','font/');
011
012 $pdf=newFPDF('L','mm','A4');
013 $pdf->Open();
014 $pdf->SetAutoPageBreak(false);
015 $pdf->AddPage();
016 $pdf->SetFont('Arial','B',12);
017 $pdf->Cell(100,6,'KEMENTERIAN HUKUM DAN HAM RI',0,0,'C');
018 $pdf->Ln();
019 $pdf->Cell(100,6,'KANTOR WILAYAH JAWA TIMUR',0,0,'C');
020 $pdf->Ln();
021 $pdf->Cell(100,6,'RUMAH TAHANAN NEGARA BANGKALAN',0,0,'C');
022 $pdf->Ln();
023 $pdf->Cell(100,6,'JL. PERTEMPURAN NO.21 BANGKALAN',0,0,'C');
024 $pdf->Ln();
025 $pdf->Cell(100,6,'TELEPON : (031)3095014',0,0,'C');
026 $pdf->Ln();
027

$pdf->Text(160,25,'DAFTAR : Narapidana yang telah


dilepaskan/dibebaskan',0);

028 $pdf->Ln();
029 $pdf->Text(181,31,"dari Rumah Tahanan Negara Bangkalan",0);
030 $pdf->Ln();
031 $bln= date("F", mktime(0, "$bulan"));
032 $pdf->Text(181,37,"Dalam bulan $bln $tahun",0);
033
034 $y_axis_initial= 45;
035 $pdf->SetFont('Arial','',10);
036 $pdf->setFillColor(222,222,222);
037 $pdf->SetY($y_axis_initial);
038 $pdf->SetX(10);
039 //Header tabel halaman 1
040 $pdf->CELL(10,6,'NO',1,0,'C',1);
041 $pdf->Cell(40,6,'NAMA',1,0,'C',1);
042 $pdf->Cell(15,6,'GOL',1,0,'C',1);
043 $pdf->Cell(15,6,'P/W',1,0,'C',1);
044 $pdf->Cell(15,6,'UMUR',1,0,'C',1);
045 $pdf->Cell(37,6,'PUTUSAN DARI',1,0,'C',1);
046 $pdf->Cell(25,6,'PERKARA',1,0,'C',1);
047 $pdf->Cell(30,6,'ASAL PENYIDIK',1,0,'C',1);
048 $pdf->Cell(30,6,'TGL BEBAS',1,0,'C',1);
049 $pdf->Cell(40,6,'ALAMAT',1,0,'C',1);
050 $pdf->Cell(25,6,'KET PIDANA',1,0,'C',1);
051 $pdf->Ln();
052 $max=25;//max baris perhalaman
053 $i=0;
054 $no=0;
055 $row_height= 6;//tinggi tiap2 cell/baris
056 $y_axis= $y_axis+ $row_height;
057 $date= date("Y-m-d");
058
059 while($row= mysql_fetch_array($qry))
060 {
061 $no++;
062 $selisih= strtotime($date) -

strtotime($row[tgl_lahir]);

063 $umur= intval($selisih/(60*60*24)/365);


if($i== $max){
//jika $i=25 maka buat header baru
064
seperti di atas
065 $pdf->AddPage();
066 $pdf->SetY(10);
067 $pdf->SetX(10);
068 $pdf->CELL(10,6,'NO',1,0,'C',1);
069 $pdf->Cell(40,6,'NAMA',1,0,'C',1);
070 $pdf->Cell(15,6,'GOL',1,0,'C',1);
071 $pdf->Cell(15,6,'P/W',1,0,'C',1);
072 $pdf->Cell(15,6,'UMUR',1,0,'C',1);

073 $pdf->Cell(37,6,'PUTUSAN DARI',1,0,'C',1);


074 $pdf->Cell(25,6,'PERKARA',1,0,'C',1);
075 $pdf->Cell(30,6,'ASAL PENYIDIK',1,0,'C',1);
076 $pdf->Cell(30,6,'TGL BEBAS',1,0,'C',1);
077 $pdf->Cell(40,6,'ALAMAT',1,0,'C',1);
078 $pdf->Cell(25,6,'KET PIDANA',1,0,'C',1);
079
080 $pdf->SetY(10);
081 $pdf->SetX(25);
082 $y_axis= $y_axis+ $row_height;
083 $i=0;
084 $pdf->Ln();
085 }
086 //tampilkan data daari database
087 $pdf->Cell(10,6,$no,1);
088 $pdf->Cell(40,6,$row[nama],1);
089 $pdf->Cell(15,6,$row[jenis_napi],1);
090 $pdf->Cell(15,6,$row[jk],1);
091 $pdf->Cell(15,6,$umur,1);
092 $pdf->Cell(37,6,$row[srt_putusan],1);
093 $pdf->Cell(25,6,$row[pasal],1);
094 $pdf->Cell(30,6,$row[asal_penyidik],1);
095 $pdf->Cell(30,6,$row[tgl_bebas],1);
096 $pdf->Cell(40,6,$row[alamat],1);
097 $pdf->Cell(25,6,'',1);
098 $pdf->Ln();
099 $i++;
100 }
101
102 //buat footer
103 $now= date("d F Y");
104 $pdf->Ln(10);
105 $pdf->SetFont('Arial','B',12);
106 $pdf->Cell(400,6,"Bangkalan, Tgl

$now",0,0,'C');

107 $pdf->Ln();
108 $pdf->Cell(404,6,"Kepala Rutan Bangkalan",0,0,'C');
109 $pdf->Ln(30);
110 $pdf->SetFont('Arial','U',12);
111 $pdf->Cell(404,6,"BAMBANG HARYANTO,Bc.IP.SH.M.Hum",0,0,'C');
112 $pdf->Ln();
113 $pdf->SetFont('Arial','',12);
114 $pdf->Cell(404,6,"NIP. 19661018 1992 03 1001",0,0,'C');
115 $pdf->Ln();
116 $pdf->Output('Napi_bebas_'.date("F Y").'.pdf', 'I');
117 ?>

Jika ada yang kurang atau menambahkan silahkan comment


2. Html2FPDF
01 <?
02 require('html2fpdf.php');
03 $pdf=newHTML2FPDF();
04 $pdf->AddPage();
05
06 //Isi PDF akan

ditampung/disimpan di variable $content

07 $content= "Ini akan dicetak ke pdf


08 <table border='1'>";
09 for($i=1;$i<=100;$i++){
10 $content.= "<tr><td>1</td><td>Nama 1</td><td>Jakarta</td></tr>
11 <tr><td>2</td><td>Nama 2</td><td>Semarang</td></tr>";
12 }
13
14 $content.="</table>";
15 $pdf->WriteHTML($content);
16 $pdf->Output("coba.pdf","I");
17 ?>

3. Html2PDF
Pertama kita buat file msf.php, file ini berisi halaman html yang akan kita konversi ke PDF.
Setau saya Html2PDF mendukung CSS sementara Html2FPDF tidak
01 <page style="font-size: 12pt">
02 <?
03 session_start();
04 mysql_connect('localhost','root','');
05 mysql_select_db('dbkapal');
06
$qry= mysql_query("select tugboat_name, cargo_id, barge_name, no_ship,
nama_rute, act_date, tot_cargo,voy from kapal a,rute b, bcr c, aktifitas
07
d where c.id_kapal = a.id_kapal and c.id_rute = b.id_rute and d.id_bcr =
c.id_bcr and attr ='complete_dish' order by barge_name ASC");
08
09 echo"<h3 align=center>Monthly Statemet of Fact</h3>
10 <table border=1 align=center>
11 <tr><td bgcolor='#999999'><strong>No</strong></td>
12 <td bgcolor='#999999'><strong>Tugboat Name</strong></td>
13 <td bgcolor='#999999'><strong>Barge Name</strong></td>
14 <td bgcolor='#999999'><strong>Cargo ID</strong></td>
15 <td bgcolor='#999999'><strong>No.Shipment</strong></td>
16 <td bgcolor='#999999'><strong>Voy</strong></td>
17 <td bgcolor='#999999'><strong>Port of Dish</strong></td>

18 <td bgcolor='#999999'><strong>Compl.Dish</strong></td>
19 <td bgcolor='#999999'><strong>Ttl Cgo(MT)</strong></td>
20 </tr>";
21
22 $no=1;
23 while($r= mysql_fetch_array($qry)){
24 echo"<tr>
25 <td>$no</td>
26 <td>$r[0]</td>
27 <td>$r[2]</td>
28 <td>$r[1]</td>
29 <td align='right'>$r[3]</td>
30 <td align='right'>$r[7]</td>
31 <td>".substr($r[4],0,16)."</td>
32 <td>".substr($r[5],0,16)."</td>
33 <td align='right'>".number_format($r[6])."</td>
34 </tr>";
35 $no++;
36 }
37 echo"</table>";
38 ?>
39 </page>

Selanjutnya kita buat file msf_pdf.php


01 ob_start();
02 include(dirname(__FILE__).'/msf.php');
03 $content= ob_get_clean();
04
05 // conversion HTML => PDF
06 require_once(dirname(__FILE__).'/html2pdf.class.php');
07 try
08 {
09 $html2pdf= newHTML2PDF('L','A4', 'fr', false, 'ISO-8859-15');
10 $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
11 $html2pdf->Output('msf.pdf');
12 }
13 catch(HTML2PDF_exception $e) { echo$e; }

Nah, dari 3 contoh library di atas. Yang manakah yang paling anda suka?

Anda mungkin juga menyukai