PHP FPDF分页符不起作用

DCJ*_*nes 5 php fpdf

我的拼贴项目结束时我正在尝试编写一个使用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();
Run Code Online (Sandbox Code Playgroud)

对此有任何帮助都很棒,我感谢你提前的时间.

附加代码

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++;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

}
Run Code Online (Sandbox Code Playgroud)

附加代码

$ _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 [评论] =>染色/变色/水垢可见/褪色 - 修复)

mer*_*ran 1

首先,您的数据有错误,所以我已经更正了您的数据并解决了您的问题(查看结果):

<?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();
?>
Run Code Online (Sandbox Code Playgroud)

您需要使用嵌套循环,因为您的数据是嵌套的。您应该使用嵌套的 foreach 循环,而不是使用 while 循环,而不是使用 while 循环。起初,变量名称令人困惑,但后来,我明白了你想要实现的目标。

用于防止每页一定数量评论的控制流程大部分是正确的。但是声明和发起时出现了拼写错误,$max_comments_per_page_ = 25;必须是$max_comments_per_page = 25;

我更改了一些数字,例如从$max_comments_per_page = 25;$max_comments_per_page = 10;,因为很难看出相关性。我已经删除了表格设计(MultiCell),因为它与这个问题无关。所以桌子的设计现在就由你决定了。

最终,FPDF 的基本(也称为强制)分页功能真正发挥了作用。问题在于你的数据和你对循环的选择。