Sei sulla pagina 1di 6

Rudiyanto

0320090020
Polman Astra
Script PHP convert ke pdf
<?php
require ('fpdf16/fpdf.php');
class PDF extends FPDF
{
function Header()
{
//Logo
$this->Image('logo_pma.jpg',10,8,20);
//Arial bold 15
$this->SetFont('Arial','B',15);
//Move to the right
$this->Cell(50);
//Title
$this->Cell(100,10,'Polman Guestbook Report',1,0,'C');
//Line break
$this->Ln(20);
}

function Footer()
{
//Position at 1,5 cm from bottom
$this->SetY(-15);
//Arial italic 8
$this->SetFont ('Arial','I',8);
//Page number
$this->Cell(0,10,'Page ' .$this->PageNo(). '/{nb}', 0, 0, 'C');
}

function LoadData()
{
//read data from database
$user = "root";
$pass = "";
$db = "dbguestbook";

$link = mysql_connect('localhost', $user, $pass);


if ( ! $link )
{
die ("Could not connect to MySQL : ".mysql_error());
}
mysql_select_db($db, $link)
or die ("Could not open $db: " .mysql_error());
$query = "Select * from tguestbook";
$result = mysql_query($query)
or die ("Select error".mysql_error());

mysql_close($link);

$data = array();
$y = 0;
while ($a_row = mysql_fetch_row($result))
{
$x = 0;
foreach ($a_row as $field){
$data[$y][$x]=stripslashes($field);
$x++;
}
$y++;
}
return $data;
}

function FancyTable($header,$data)
{
//Colors, line width and bold font
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//Header
$w=array(30,50,110);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
//Color and font restoration
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill=false;
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,$row[2],'LR',0,'R',$fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w),0,'','T');
}

}
$pdf = new PDF();
//judul kolom
$header = array('Nama','Email','Komentar');
//data loading
$data=$pdf->LoadData();
$pdf->SetFont('Arial','',12);
$pdf->AddPage();
$pdf->SetFillColor(0,255,0);
$pdf->FancyTable($header, $data);
$pdf->Output();

?>

Output
Script PHP convert ke pdf

<?php
// nama file
$namaFile = "reportguestbook.csv";

// Function penanda awal file (Begin Of File) Excel

function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}

// Function penanda akhir file (End Of File) Excel

function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}

// Function untuk menulis data (angka) ke cell excel

function xlsWriteNumber($Row, $Col, $Value) {


echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}

// Function untuk menulis data (text) ke cell excel

function xlsWriteLabel($Row, $Col, $Value ) {


$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}

// header file excel

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,
pre-check=0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

// header untuk nama file


header("Content-Disposition: attachment;
filename=".$namaFile."");

header("Content-Transfer-Encoding: binary ");

// memanggil function penanda awal file excel


xlsBOF();

// ------ membuat kolom pada excel --- //

// mengisi pada cell A1 (baris ke-0, kolom ke-0)


xlsWriteLabel(0,0,"Nama");

// mengisi pada cell A2 (baris ke-0, kolom ke-1)


xlsWriteLabel(0,1,"Email");

// mengisi pada cell A3 (baris ke-0, kolom ke-2)


xlsWriteLabel(0,2,"Komentar");

// -------- menampilkan data --------- //

// koneksi ke mysql
mysql_connect("localhost", "root", "");
mysql_select_db("dbGuestbook");

// query menampilkan semua data

$query = "SELECT * FROM tGuestbook order by nama";


$hasil = mysql_query($query);

// nilai awal untuk baris cell


$noBarisCell = 1;

while ($data = mysql_fetch_array($hasil))


{

// menampilkan data nama


xlsWriteLabel($noBarisCell,0,$data['nama']);

// menampilkan data email


xlsWriteLabel($noBarisCell,1,$data['alamat']);

// menampilkan data komentar


xlsWriteLabel($noBarisCell,2,$data['komentar']);

// increment untuk no. baris cell dan no. urut data


$noBarisCell++;

// memanggil function penanda akhir file excel


xlsEOF();
exit();

?>

Potrebbero piacerti anche