|
|
|
ขอความช่วยเหลือใน FPDF ให้แสดง Header แบบนี้ต้องเพิ่มยังไงครับ |
|
|
|
|
|
|
|
ขอสอบถามครับ
ใน FPDF จะให้แสดงหัวตารางแบบนี้ต้องเพิ่มยังไงครับ ตามรูป
ตอนนี้ที่ทำออกมามันแสดงแบบนี้ครับ
Code
Code (PHP)
<html>
<head>
<title>test pdf</title>
</head>
<body>
<?php
define('FPDF_FONTPATH','font/');
require('pdf/fpdf.php');
class PDF extends FPDF
{
function Footer()
{
$this->AddFont('angsa','','angsa.php');
$this->AddFont('angsa','B','angsab.php');
//$pdf->SetFont('angsa','',10);
// $pdf->SetFont('angsa','B',12);
$this->SetY(-15);
$this->SetFont('angsa','B',8);
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
//Load data
function LoadData($file)
{
//Read file lines
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(';',chop($line));
return $data;
}
//Better table
function ImprovedTable($header,$data)
{
//Column widths
$w=array(46,18,18,18,18,18,18,18,20);
//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C');
$this->Ln();
//Data
foreach ($data as $eachResult)
{
$this->Cell(46,6,iconv( 'UTF-8','TIS-620',$eachResult["ItemCode"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH1-FG"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH3-FG"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH1-RENT"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH3-RENT"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH1-PRO"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH3-PRO"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH3-RE"]),1);
$this->Cell(18,6,iconv( 'UTF-8','TIS-620',$eachResult["WH3-REJ"]),1);
$this->Ln();
}
//Closure line
$this->Cell(array_sum($w),0,'','T');
}
//Colored table
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(46,18,18,18,18,18,18,18,18);
// 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;
$n=1;
foreach($data as $row)
{
if ($n % 43 === 1)
{
$this->AddPage();
//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(46,18,18,18,18,18,18,18,18);
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('');
}
$this->Cell($w[0],6,$row[0],'LR',0,'R',$fill);
$this->Cell($w[1],6,number_format($row[1]),'LR',0,'R',$fill);
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
$this->Cell($w[4],6,number_format($row[3]),'LR',0,'R',$fill);
$this->Cell($w[5],6,number_format($row[4]),'LR',0,'R',$fill);
$this->Cell($w[6],6,number_format($row[5]),'LR',0,'R',$fill);
$this->Cell($w[7],6,number_format($row[6]),'LR',0,'R',$fill);
$this->Cell($w[8],6,number_format($row[7]),'LR',0,'R',$fill);
$this->Cell($w[9],6,number_format($row[8]),'LR',0,'R',$fill);
$this->Ln();
$fill=!$fill;
++$n;
}
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF();
//Column titles
$header=array('Description','WH1-FG','WH3-FG','WH1-RENT','WH3-RENT','WH1-PRO','WH3-PRO','WH3-RE','WH3-REJ');
//Data loading
//*** Load MySQL Data ***//
$objConnect = mssql_connect("192.168.1.3\SQL2005","sa","1234") or die("Error Connect to Database");
$objDB = mssql_select_db("Golive");
$strSQLx = "SELECT
T0.ItemCode,
sum(case T1.WhsCode when 'WH1-FG' then T1.OnHand else 0 end) as 'WH1-FG',
sum(case T1.WhsCode when 'WH1-PRO' then T1.OnHand else 0 end) as 'WH1-PRO',
sum(case T1.WhsCode when 'WH1-RENT' then T1.OnHand else 0 end) as 'WH1-RENT',
sum(case T1.WhsCode when 'WH3-FG' then T1.OnHand else 0 end) as 'WH3-FG',
sum(case T1.WhsCode when 'WH3-PRO' then T1.OnHand else 0 end) as 'WH3-PRO',
sum(case T1.WhsCode when 'WH3-RE' then T1.OnHand else 0 end) as 'WH3-RE',
sum(case T1.WhsCode when 'WH3-REJ' then T1.OnHand else 0 end) as 'WH3-REJ',
sum(case T1.WhsCode when 'WH3-RENT' then T1.OnHand else 0 end) as 'WH3-RENT'
FROM
OITM T0 INNER JOIN OITW T1 ON T0.ItemCode = T1.ItemCode
WHERE
T1.OnHand <> '0' and T1.WhsCode In ('WH1-FG','WH1-PRO','WH1-RENT','WH3-FG','WH3-PRO','WH3-RE', 'WH3-REJ','WH3-RENT') AND
(T0.ItmsGrpCod in ('111','104','105')) and (T0.itemCode not like '%FINISHER%' and T0.itemCode not like '%A3%'and T0.itemCode not like '%IMAGEPASS%')
GROUP BY
T0.ItemCode
ORDER by T0.ItemCode";
$objQuery = mssql_query($strSQLx) or die ("Error Query [".$strSQLx."]");
$resultData = array();
for ($i=0;$i<mssql_num_rows($objQuery);$i++) {
$result = mssql_fetch_array($objQuery);
array_push($resultData,$result);
}
//************************//
$pdf->AddFont('angsa','','angsa.php');
$pdf->AddFont('angsa','B','angsab.php');
$pdf->SetFont('angsa','',10);
$pdf->SetFont('angsa','B',12);
//*** Table 3 ***//
//$pdf->AddPage();
//$pdf->Image('pdf/logo.png',80,8,33);
//$pdf->Ln();
$pdf->FancyTable($header,$resultData);
$pdf->Output("pdf/MyPDF/MyPDF.pdf","F");
// Settings
$name = "Name goes here";
$email = "[email protected]";
$to = "$email";
$from = "[email protected]";
$subject = "รายงานเครื่อง สถานะต่างๆ";
$mainMessage = "Hi, here's the file.";
$fileatt = "pdf/MyPDF/MyPDF.pdf";
$fileatttype = "application/pdf";
$fileattname = "RYG_Item.pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
///
//*** Table 1 ***//
//$pdf->AddPage();
//$pdf->Image('pdf/logo.png',80,8,33);
//$pdf->Ln(35);
//$pdf->BasicTable($header,$resultData);
//*** Table 2 ***//
//$pdf->AddPage();
//$pdf->Image('pdf/logo.png',80,8,33);
//$pdf->Ln(35);
//$pdf->ImprovedTable($header,$resultData);
?>
<?
//PDF Created Click <a href="../Production/pdf/MyPDF/MyPDF.pdf">here</a> to Download
?>
</body>
</html>
Tag : PHP, Ms SQL Server 2005
|
|
|
|
|
|
Date :
2016-01-02 01:35:26 |
By :
karurub |
View :
1065 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ตัวนี้จัดรูปแบบยากมากครับ ลองเปลี่ยนไปใช้ mPDF นะครับ สามารถแปลงจาก HTML เป็น PDF ได้ครับ
|
|
|
|
|
Date :
2016-01-03 11:56:26 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|