[英]PHP FPDF page break not working
我的拼貼項目結束時我正在嘗試編寫一個使用FPDF生成PDF文件的腳本。
該腳本應查詢數據表並從一個表返回作業數據,並從另一個表返回作業注釋。 這一切都有效。
然后我按行輸出每個作業,每頁最多25行(橫向)。 這一切都有效。 在輸出所有作業之后,我需要在作業數據下面輸出注釋數據。
這有效,但評論數據不會在頁面底部之前分頁。 任何人都可以看到我出錯的地方。 在我認為錯誤的地方,我用// *****標記了下面的腳本。 我知道劇本很長但我認為它可以更好地解釋我的問題。
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(false);
$pdf->AddPage(L);
$y_axis_initial = 40;
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 25;
$y_axis = 40;
//Set Row Height
$row_height = 6;
$y_axis = $y_axis + $row_height;
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('Arial','B',9);
$pdf->SetY(40);
$pdf->SetX(10);
$pdf->Cell(13,6,'SeqID',1,0,'L',1);
$pdf->SetX(23);
$pdf->Cell(150,6,'Sequence',1,0,'L',1);
$pdf->SetX(150);
$pdf->Cell(110,6,'Item',1,0,'L',1);
$pdf->SetX(255);
$pdf->Cell(25,6,'Status',1,0,'L',1);
$column_seqid = "";
$column_headerid = "";
$column_seq = "";
$column_status = "";
//For each row, add the field to the corresponding column
while ($row_Audits = mysql_fetch_assoc($Audits))
{
if ($i == $max)
{
$pdf->AddPage('L');
$newY = $pdf->GetY();
$y_axis = 40;
$pdf->SetFillColor(232,232,232);
//print column titles for the current page
$pdf->SetFont('Arial','B',9);
$pdf->SetY(40);
$pdf->SetX(10);
$pdf->Cell(13,6,'SeqID',1,0,'L',1);
$pdf->SetX(23);
$pdf->Cell(150,6,'Sequence',1,0,'L',1);
$pdf->SetX(150);
$pdf->Cell(110,6,'Item',1,0,'L',1);
$pdf->SetX(255);
$pdf->Cell(25,6,'Status',1,0,'L',1);
//Go to next row
$y_axis = $y_axis + $row_height;
//Set $i variable to 0 (first row)
$i = 0;
}
if(strlen($row_Audits['SeqNo']) <4)
{
$SeqID = "0".$row_Audits['SeqNo'];
} else {
$SeqID = $row_Audits['SeqNo'];
}
$column_seqid = $SeqID;
$column_headerid = $row_Audits['SeqHeader'];
if($row_Audits['SeqNo'] =="1306")
{
$Seq = $row_Audits['SeqText'] . " " . $row_Audits['WaterHot'];
}elseif($row_Audits['SeqNo'] =="1307")
{
$Seq = $row_Audits['SeqText'] . " " . $row_Audits['WaterCold'];
} else {
$Seq = $row_Audits['SeqText'];
}
$column_seq = $Seq;
if($row_Audits['Status'] == "")
{
$Status = "Pass";
}elseif($row_Audits['Status'] == "1")
{
$Status = "Fail";
}elseif($row_Audits['RepairCode'] == "4")
{
$Status = "Repaired";
}elseif($row_Audits['Status'] == "3")
{
$Status = "Fixed";
}elseif($row_Audits['Status'] === "")
{
$Status = "Required";
}
$column_status = $Status;
$pdf->SetFont('Arial','',8);
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->Cell(13,6,$column_seqid,1);
$pdf->SetX(23);
$pdf->Cell(127,6,$column_headerid,1,'L');
$pdf->SetX(150);
$pdf->Cell(105,6,$column_seq,1,'L');
$pdf->SetX(255);
$pdf->Cell(25,6,$column_status,1,'L');
$y_axis = $y_axis + $row_height;
$i = $i + 1;
}
//*******************************************************************
// Up to this point the output is corrent in it's format. I then need to display the data concerning any comments.
// But when the PDF is displayed the comments data will not break at the botton of the page
if($totalRows_Comments > 0)
{
$column_comments = "";
$column_seq_comments = "";
while ($row_Comments = mysql_fetch_assoc($Comments)) {
$SeqComments = substr($row_Comments['SeqID'], 5);
$CommentsText = $row_Comments['Comments'];
$column_seq_comments = $column_seq_comments.$SeqComments."\n";
$column_comments = $column_comments.$CommentsText."\n";
}
//Create lines (boxes) for each ROW (Product)
//If you don't use the following code, you don't create the lines separating each row
$pdf->SetFont('Arial','B',10);
$newY = $pdf->GetY();
$y_axis = $newY + 28;
$pdf->Ln(10);
$pdf->SetFont('Arial','',8);
$pdf->SetY($y_axis - 10);
$pdf->SetX(10);
$pdf->Cell(64,6,'Auditors comments',0,0,'L',0);
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->MultiCell(10,6,$column_seq_comments,1);
$pdf->SetY($y_axis);
$pdf->SetX(20);
$pdf->MultiCell(260,6,$column_comments,1);
while ($i < $totalRows_Comments)
{
$pdf->SetX(10);
$pdf->MultiCell(194,6,'',1);
$i = $i +1;
}
}
$pdf->Ln();
$newY = $pdf->GetY();
$y_axis = $newY + 5;
$pdf->SetFont('Arial','B',10);
$pdf->SetX(10);
$pdf->MultiCell(81,6,'Engineers comments:',0);
$newY = $pdf->GetY();
$y_axis = $newY + 5;
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->MultiCell(81,6,'Engineer:....................................',0);
$newY = $pdf->GetY();
$y_axis = $newY + 5;
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->Cell(81,6,'Repair date:....................................',0);
$pdf->Output();
對此的任何幫助都會很棒,我感謝您提前的時間。
附加代碼
if($totalRows_Comments > 0) {
$column_comments = "";
$column_seq_comments = "";
$max_comments_per_page_ = 25;
$pdf->SetFont('Arial','B',10);
$newY = $pdf->GetY();
$pdf->Ln(10);
$pdf->SetFont('Arial','',8);
$pdf->SetY($newY + 10);
$pdf->SetX(10);
$pdf->Cell(64,6,'Auditors comments',0,0,'L',0);
while ($row_Comments = mysql_fetch_assoc($Comments)) {
if ($j == $max_comments_per_page) {
$j = 1;
$pdf->AddPage('L');
$pdf->Ln(10);
$pdf->SetFont('Arial','',8);
$pdf->SetY(50);
$pdf->SetX(10);
$pdf->Cell(64,6,'Auditors comments',0,0,'L',0);
}
$SeqID = preg_replace("/[^0-9,.]/", "", $row_Comments['SeqID']);
if(strlen($SeqID) <4) {
$SeqComments = "0".$SeqID;
} else {
$SeqComments = $SeqID;
}
$CommentsText = $row_Comments['Comments'];
$column_seq_comments = $column_seq_comments.$SeqComments."\n";
$column_comments = $column_comments.$CommentsText."\n";
//$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->MultiCell(10,6,$column_seq_comments,1);
//$pdf->SetY($y_axis);
$pdf->SetX(20);
$pdf->MultiCell(260,6,$column_comments,1);
$j++;
}
}
附加代碼
$ _row_comments數組包含。 例:
數組([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID110 [評論] =>缺口 - 划痕 - 染色 - 需要繪畫)數組([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID203 [評論] =>房間門把手/打擊板 - 不安全/不工作安全門鏈 - 不工作室門鎖死 - 未正確操作)陣列([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID304 [評論] =>單位 - 嘈雜 - 不工作)數組([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID404 [評論] =>門鉸鏈 - 吱吱聲/棒 - 需要油/修復)數組([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID502 [評論] =>門把手/打擊板 - 不安全/不工作)陣列([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID1411 [評論] =>點擊 - 不安全/泄漏 - 修復彈出塞子或插頭 - 需要調整布線 - 損壞/破損 - 修復浴室面板 - 損壞 - 修復不良的破裂灌漿/密封膠/硅/帶 - 修復淋浴噴頭 - 曝氣器de -Scaled)數組([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID1305 [評論] =>染色/變色/水垢可見/褪色 - 修復)
首先,您的數據有問題,因此我更正了您的數據並解決了您的問題( 請參閱結果 ):
<?php
require('./fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Auditors comments');
$row_Comments = [Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID110", "Comments" => ["Chipped", "Scratched", "Stained", "Needs Paint"]),
Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID203", "Comments" => ["Room Door Handle/Strike plate", "Not Secure/Not Working Security Door Chain", "Not Working Room Door Dead Lock", "Not operating Correctly"] ),
Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID304", "Comments" => ["Unit", "Noisy", "Not Working"] ),
Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID404", "Comments" => ["Door Hinges", "Squeaks/Sticks", "Requires Oil/Repair"] ),
Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID502", "Comments" => ["Door Handle/Strike plate", "Not secure/Not Working"] ),
Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID1411", "Comments" => ["Taps", "Not Secure/Leaking", "Repair Pop Up Stoppers or Plug", "Requires Adjustment Cloths Line", "Damaged/Broken", "Repair Bath Panel", "Damaged", "Repair Poor Cracked Grouting/Sealant/Silicon/Strip", "Repair Shower head", "Aerator de-Scaled"] ),
Array ( "UniqueID" => "NXLHR01071474538755", "SeqID" => "SeqID1305", "Comments" => ["Stained / Discoloured / Limescale Visible/ Tarnished", "Repair"] )];
$max_comments_per_page = 10;
$pdf->SetFont('Arial','',8);
//echo $row_Comments[0]["Comments"];
$j = 1;
foreach($row_Comments as $row_Comment) {
foreach($row_Comment["Comments"] as $comment) {
//echo $j;
if ($j >= $max_comments_per_page) {
$j = 1;
$pdf->AddPage();
$pdf->Ln(10);
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Auditors comments');
$pdf->SetFont('Arial','',8);
}
$pdf->Ln(10);
$pdf->Cell(40,10,$comment);
$j++;
}
}
$pdf->Output();
?>
您需要使用嵌套循環,因為您的數據是嵌套的。 您應該使用嵌套的foreach循環,而不是使用while 循環 。 起初,變量名稱令人困惑,但后來我明白了你想要實現的目標。
每頁阻止一定數量評論的控制流程大多是正確的。 但是有一個錯字用於聲明和啟動$max_comments_per_page_ = 25;
必須是$max_comments_per_page = 25;
我更改了一些數字,例如$max_comments_per_page = 25;
至$max_comments_per_page = 10;
因為很難看出相關性。 我已經刪除了表格設計(MultiCell),因為這個問題與主題無關。 所以桌子設計由你決定。
最終,FPDF的基本(又稱強制)分頁功能實際上正在運行。 問題是你的數據和你選擇的循環。
要啟用或禁用自動分頁模式,我們可以使用SetAutoPageBreak()
方法。
SetAutoPageBreak(boolean auto [, float margin])
這里第二個參數是距頁面底部的距離,該距離定義了觸發限制。 默認情況下,模式打開,邊距為2厘米。
你設置了$pdf->SetAutoPageBreak(false);
因此,您無法在頁面中獲得分頁符。
請從腳本中刪除此行,您將獲得換行符;
請在此處查看更多信息
編輯
請嘗試下面的代碼注釋部分
require('fpdf.php');
ini_set('display_errors',1);
$pdf = new FPDF();
$pdf->AddPage('L');
$y_axis_initial = 40;
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 25;
$y_axis = 40;
//Set Row Height
$row_height = 6;
$y_axis = $y_axis + $row_height;
$column_seqid = "";
$column_headerid = "";
$column_seq = "";
$column_status = "";
$Comments = array(
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID110', 'Comments' => ' Chipped - Scratched - Stained - Needs Paint' ),
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID203', 'Comments' => ' Room Door Handle/Strike plate - Not Secure/Not Working Security Door Chain - Not Working Room Door Dead Lock - Not operating Correctly' ),
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID304', 'Comments' => ' Unit - Noisy - Not Working' ),
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID404', 'Comments' => ' Door Hinges - Squeaks/Sticks - Requires Oil/Repair' ),
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID502', 'Comments' => ' Door Handle/Strike plate - Not secure/Not Working' ),
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID1411', 'Comments' => ' Taps - Not Secure/Leaking - Repair Pop Up Stoppers or Plug - Requires Adjustment Cloths Line - Damaged/Broken - Repair Bath Panel - Damaged - Repair Poor Cracked Grouting/Sealant/Silicon/Strip - Repair Shower head - Aerator de-Scaled' ),
//Please uncomment this line to check multiple pages
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID1305', 'Comments' => ' Stained / Discoloured / Limescale Visible/ Tarnished - RepairTaps - Not Secure/Leaking - Repair Pop Up Stoppers or Plug - Requires Adjustment Cloths Line - Damaged/Broken - Repair Bath Panel - Damaged - Repair Poor Cracked Grouting/Sealant/Silicon/Strip - Repair Shower head - Aerator de-ScaledTaps - Not Secure/Leaking - Repair Pop Up Stoppers or Plug - Requires Adjustment Cloths Line - Damaged/Broken - Repair Bath Panel - Damaged - Repair Poor Cracked Grouting/Sealant/Silicon/Strip - Repair Shower head - Aerator de-ScaledTaps - Not Secure/Leaking - Repair Pop Up Stoppers or Plug - Requires Adjustment Cloths Line - Damaged/Broken - Repair Bath Panel - Damaged - Repair Poor Cracked Grouting/Sealant/Silicon/Strip - Repair Shower head - Aerator de-ScaledTaps - Not Secure/Leaking - Repair Pop Up Stoppers or Plug - Requires Adjustment Cloths Line - Damaged/Broken - Repair Bath Panel - Damaged - Repair Poor Cracked Grouting/Sealant/Silicon/Strip - Repair Shower head - Aerator de-Scaled' ),
Array ( 'UniqueID' => ' NXLHR01071474538755 ', 'SeqID' => ' SeqID1305', 'Comments' => ' Stained / Discoloured / Limescale Visible/ Tarnished - Repair' )
);
$totalRows_Comments = count($Comments);
if($totalRows_Comments > 0)
{
$column_comments = "";
$column_seq_comments = "";
$pdf->SetFont('Arial','B',10);
$newY = $pdf->GetY();
$y_axis = $newY + 28;
$pdf->Ln(10);
$pdf->SetY($y_axis - 10);
$pdf->SetX(10);
$pdf->Cell(64,6,'Auditors comments',0,0,'L',0);
$pdf->SetFont('Arial','',8);
$pdf->Line(10,$y_axis-1,280,$y_axis-1);
//Loop all comments
foreach( $Comments as $row_Comments) {
$SeqComments = substr(trim($row_Comments['SeqID']), 5);
$CommentsText = trim($row_Comments['Comments']);
$column_seq_comments = $SeqComments."\n";
$column_comments = $CommentsText."\n";
$comment_length = strlen($column_comments);
$length = ceil($comment_length/260);
if ( $h = $pdf->GetPageHeight() ) {
//Check for comment length is not large than available space in page
if ( $y_axis > floor($h) || ( $h - $y_axis ) < ceil($comment_length / 260 * 8 + 16) ) {
//Add new page
$pdf->AddPage('L');
//Reset Y position
$y_axis = 40;
//Add line on comment startup
$pdf->Line(10,$y_axis,280,$y_axis);
}
}
//Add SeqID
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->MultiCell(10,6,$column_seq_comments);
//Add Comment
$pdf->SetY($y_axis);
$pdf->SetX(20);
$pdf->MultiCell(260,6,$column_comments);
//Change y value for multiline comments
if($comment_length > 200) {
$y_axis += 8 * $length - 2;
$line_height = $y_axis;
}
//Default height
$y_axis += 8;
//Draw line after comment
$pdf->Line(10,$y_axis-1,280,$y_axis-1);
}
//The below code will produce blank box in your page if don't want than delete below code
//Check page height for blank comment box
if ( ($h - $y_axis) < $totalRows_Comments * 10 ) {
$pdf->AddPage('L');
$y_axis = 40;
$pdf->SetY($y_axis);
} else {
$pdf->Ln(10);
}
//Create lines (boxes) for each ROW (Product)
while ($i < $totalRows_Comments)
{
$pdf->SetX(10);
$pdf->MultiCell(270,6,'',1);
$i = $i +1;
}
//Blank box ended
}
$newY = $pdf->GetY();
$y_axis = $newY + 5;
//Check page height for blank comment box
if ( ($h - $y_axis) < 40 ) {
$pdf->AddPage('L');
$y_axis = 40;
$pdf->SetY($y_axis);
} else {
$pdf->Ln(10);
}
$pdf->SetFont('Arial','B',10);
$pdf->SetX(10);
$pdf->MultiCell(81,6,'Engineers comments:',0);
$newY = $pdf->GetY();
$y_axis = $newY + 5;
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->MultiCell(81,6,'Engineer:....................................',0);
$newY = $pdf->GetY();
$y_axis = $newY + 5;
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->Cell(81,6,'Repair date:....................................',0);
$pdf->Output();
注意:如果不想要空白框而不是從腳本中刪除代碼。
根據問題,它通常發生在php程序員身上。 我認為FPDF如何強制分頁這個anwer可能對你非常有用。 你應該嘗試一下。 上次我使用fpdf然后我用數據庫創建了一個簡單的pdf。 它的工作很好。 也像FPDF一樣,您也可以使用MPDF生成pdf。 嘗試使用鏈接的答案。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.