ขอสอบถามการแบ่งหน้าของ fpdf ว่าทำอย่างไร แล้วขึ้นหน้าใหม่ให้มีหัวข้อด้วย
ขอสอบถามครับ
ผมลองทำตาม
https://www.thaicreate.com/php/forum/045367.html
แต่ก็ยัง งงๆ อยู่ไม่รู้ว่าจะแบ่งหน้ายังไง แบบตัวอย่างข้อมูลตายตัว 50 row แต่ของผมไม่ตายตัว แต่อยากให้แสดงหน้าละ 40 record แล้วขึ้นหน้าใหม่ ไม่รู้จาก Code ด้านล่างต้องเพิ่มเข้าไปตรงส่วนไหนครับ ช่วยทีครับ
Code (PHP)
<html>
<head>
<title>xxxd</title>
</head>
<body>
<?php
define('FPDF_FONTPATH','font/');
require('pdf/fpdf.php');
class PDF extends FPDF
{
//function Footer()
//{
// $this->SetY(-15);
// $this->SetFont('Arial','|',8);
// $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',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;
foreach($data as $row)
{
$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;
}
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF();
//Column titles
$header=array('Descriptinon','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 = "admin@abccom";
$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.";
}
?>
<?
//PDF Created Click <a href="../Production/pdf/MyPDF/MyPDF.pdf">here</a> to Download
?>
</body>
</html>
Tag : PHP, Ms SQL Server 2005
Date :
2015-12-24 23:51:49
By :
karurub
View :
1437
Reply :
5
ใช้ $pdf->AddPage(); ครับ สร้างตัวแปรขึ้นมานับ row พอครบ 40 ก็ AddPage();
Date :
2015-12-25 08:34:39
By :
amphol-yui
ขอบคุณครับ ช่วยแนะนำด้วยนะครับ ว่าต้องแทรกยังไง
Date :
2015-12-25 09:48:26
By :
karurub
Code (PHP)
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 % 40 === 1)
{
$pdf->AddPage();
//เอา Header แสดงที่นี่
//อย่าลืม Set XY ที่นี่ด้วยนะ
}
$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');
}
ประวัติการแก้ไข 2015-12-25 10:50:00
Date :
2015-12-25 10:49:22
By :
fossil31
ตอบความคิดเห็นที่ : 3 เขียนโดย : fossil31 เมื่อวันที่ 2015-12-25 10:49:22
รายละเอียดของการตอบ ::
ลองแล้วนะครับ แต่ขึ้น error
Fatal error: Call to a member function AddPage() on a non-object in C:\AppServ\www\Report\Production\inv01pdf.php on line 99
Code (PHP)
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 % 40 ===1)
{
$pdf->AddPage();
//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();
}
$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');
}
Date :
2015-12-26 01:04:42
By :
karurub
Load balance : Server 01