Selasa, 10 Juni 2014

Membuat Laporan PDF dengan PHP dan MySQL

1. inc-db.php
2. index1.php
3. cetak_pdf.php
4. donwload library disini


1. inc-db.php





2. index1.php

<?php
include('../inc-db.php');
error_reporting(0);
$sqlShow="SELECT * FROM tb_mahasiswa ";
$result=mysql_query($sqlShow);
?>
<table border='1'><b><code><a href="cetak_pdf.php">Cetak PDF</a></code></b>
<tr><td><b>No</b></td><td><b>Nim</b></td><td><b>Nama</b></td><td><b>Alamat</b></td></tr>
<?php
$no=$posisi+0;
while($data=mysql_fetch_array($result)){
$no++;
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $data['nim']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><?php echo $data['alamat']; ?></td>
</tr>
<?php
}
?>
</table>

3. cetak_pdf.php

<?php
include('../inc-db.php');


#ambil data di tabel dan masukkan ke array
$query = "SELECT * FROM tb_mahasiswa ORDER BY id";
$sql = mysql_query ($query);
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
array_push($data, $row);
}

#setting judul laporan dan header tabel
$judul = "LAPORAN DATA MAHASISWA";
$header = array(
array("label"=>"No", "length"=>30, "align"=>"L"),
array("label"=>"Nim", "length"=>50, "align"=>"L"),
array("label"=>"Nama", "length"=>80, "align"=>"L"),
array("label"=>"Alamat", "length"=>30, "align"=>"L")
);

#sertakan library FPDF dan bentuk objek
require_once ("fpdf16/fpdf16.php");
$pdf = new FPDF();
$pdf->AddPage();

#tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,20, $judul, '0', 1, 'C');

#buat header tabel
$pdf->SetFont('Arial','','10');
$pdf->SetFillColor(24,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
foreach ($header as $kolom) {
$pdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
}
$pdf->Ln();

#tampilkan data tabelnya
$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('');
$fill=false;
foreach ($data as $baris) {
$i = 0;
foreach ($baris as $cell) {
$pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
$i++;
}
$fill = !$fill;
$pdf->Ln();
}

#output file PDF
$pdf->Output();

1 komentar:

Nulla nunc dui, tristique in semper vel

Nulla nunc dui, tristique in semper vel, congue sed ligula. Nam dolor ligula, faucibus id sodales in, auctor fringilla libero. Pellentesqu...