001.
<html>
002.
<head>
003.
<title>ThaiCreate.Com PHP PDF</title>
004.
</head>
005.
<body>
006.
007.
<?php
008.
require
(
'fpdf.php'
);
009.
010.
class
PDF
extends
FPDF
011.
{
012.
013.
function
LoadData(
$file
)
014.
{
015.
016.
$lines
=file(
$file
);
017.
$data
=
array
();
018.
foreach
(
$lines
as
$line
)
019.
$data
[]=
explode
(
';'
,
chop
(
$line
));
020.
return
$data
;
021.
}
022.
023.
024.
025.
function
ImprovedTable(
$header
,
$data
)
026.
{
027.
028.
$w
=
array
(20,30,55,25,25,25);
029.
030.
for
(
$i
=0;
$i
<
count
(
$header
);
$i
++)
031.
$this
->Cell(
$w
[
$i
],7,
$header
[
$i
],1,0,
'C'
);
032.
$this
->Ln();
033.
034.
035.
foreach
(
$data
as
$eachResult
)
036.
{
037.
$this
->Cell(20,6,
$eachResult
[
"CustomerID"
],1);
038.
$this
->Cell(30,6,
$eachResult
[
"Name"
],1);
039.
$this
->Cell(55,6,
$eachResult
[
"Email"
],1);
040.
$this
->Cell(25,6,
$eachResult
[
"CountryCode"
],1,0,
'C'
);
041.
$this
->Cell(25,6,number_format(
$eachResult
[
"Budget"
],2),1,0,
'R'
);
042.
$this
->Cell(25,6,number_format(
$eachResult
[
"Budget"
],2),1,0,
'R'
);
043.
$this
->Ln();
044.
}
045.
046.
047.
$this
->Cell(
array_sum
(
$w
),0,
''
,
'T'
);
048.
}
049.
050.
051.
function
FancyTable(
$header
,
$data
)
052.
{
053.
054.
055.
foreach
(
$data
as
$row
)
056.
{
057.
$this
->Cell(
$w
[0],6,
$row
[0],
'LR'
,0,
'L'
,
$fill
);
058.
$this
->Cell(
$w
[1],6,
$row
[1],
'LR'
,0,
'L'
,
$fill
);
059.
$this
->Cell(
$w
[2],6,
$row
[2],
'LR'
,0,
'L'
,
$fill
);
060.
$this
->Cell(
$w
[3],6,
$row
[3],
'LR'
,0,
'C'
,
$fill
);
061.
$this
->Cell(
$w
[4],6,number_format(
$row
[4]),
'LR'
,0,
'R'
,
$fill
);
062.
$this
->Cell(
$w
[5],6,number_format(
$row
[5]),
'LR'
,0,
'R'
,
$fill
);
063.
$this
->Ln();
064.
$fill
=!
$fill
;
065.
}
066.
$this
->Cell(
array_sum
(
$w
),0,
''
,
'T'
);
067.
}
068.
}
069.
070.
$pdf
=
new
PDF();
071.
072.
$header
=
array
(
'ssss'
,
'Name'
,
'Email'
,
'Country Code'
,
'Budget'
,
'Used'
);
073.
074.
075.
076.
$objConnect
= mysql_connect(
"localhost"
,
"root"
,
"1234"
)
or
die
(
"Error Connect to Database"
);
077.
$objDB
= mysql_select_db(
"mydatabase"
);
078.
$strSQL
=
"SELECT * FROM customer"
;
079.
$objQuery
= mysql_query(
$strSQL
);
080.
$resultData
=
array
();
081.
for
(
$i
=0;
$i
<mysql_num_rows(
$objQuery
);
$i
++) {
082.
$result
= mysql_fetch_array(
$objQuery
);
083.
array_push
(
$resultData
,
$result
);
084.
}
085.
086.
087.
088.
089.
$pdf
->SetFont(
'Arial'
,
''
,10);
090.
091.
092.
093.
$pdf
->AddPage();
094.
$pdf
->Image(
'logo.png'
,80,8,33);
095.
$pdf
->Ln(35);
096.
$pdf
->ImprovedTable(
$header
,
$resultData
);
097.
098.
099.
$pdf
->Output(
"MyPDF/MyPDF.pdf"
,
"F"
);
100.
?>
101.
102.
ดาวโหลด <a href=
"MyPDF/MyPDF.pdf"
>here</a>
103.
</body>
104.
</html>