【问题标题】:PHP FPDF page break not workingPHP FPDF分页符不起作用
【发布时间】:2016-09-13 08:11:28
【问题描述】:

我的拼贴项目即将结束,我现在正在尝试编写一个使用 FPDF 生成 PDF 文件的脚本。

该脚本应查询数据表并从一个表返回作业数据并从另一表返回作业 cmets。这一切都有效。

然后我以每页最多 25 行的行输出每个作业(横向)。同样,所有这些都有效。输出所有作业后,我需要在作业数据下方输出 cmets 数据。

这可行,但 cmets 数据不会在页面底部之前分页。任何人都可以看到我哪里出错了。我在我认为错误的地方用 //***** 标记了下面的脚本。我知道脚本很长,但我认为它可以更好地解释我的问题。

$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_cmets 数组包含。示例:

Array ([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID110 [Comments] => 碎裂 - 划痕 - 染色 - 需要油漆) Array ( [UniqueID] => NXLHR01071474538755 [SeqID] => SeqID203 [Comments] =>房间门把手/防撞板 - 不安全/不工作 安全门链 - 不工作 房间门死锁 - 未正确操作 ) 阵列 ( [UniqueID] => NXLHR01071474538755 [SeqID] => SeqID304 [Comments] => 单位 - 嘈杂 -不工作)阵列([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID404 [评论] => 门铰链 - 吱吱声/棒 - 需要油/维修)阵列([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID502 [评论] => 门把手/防撞板 - 不安全/不工作 ) 阵列 ( [UniqueID] => NXLHR01071474538755 [SeqID] => SeqID1411 [评论] => 水龙头 - 不安全/泄漏 - 修复弹出塞或插头 - 需要调整布线 - 损坏/破损 - 修复浴缸面板 - 损坏 - 修复不良开裂的灌浆/密封胶/硅/条 - 修复淋浴头 - 曝气器de-Scaled ) Array ( [UniqueID] => NXLHR01071474538755 [SeqID] => SeqID1305 [Comments] => 染色/变色/水垢可见/失去光泽 - 修复)

【问题讨论】:

  • $pdf-&gt;AddPage(L); L 应该用引号引起来(在代码的顶部)。
  • 您需要使用行而不是手动添加单元格。如果启用 $pdf->SetAutoPageBreak。它会自动添加分页符。
  • @Martin 感谢您的评论,错过了该评论,但仍然存在布局问题。再次非常感谢。

标签: php fpdf


【解决方案1】:

To启用或禁用自动分页模式我们可以使用SetAutoPageBreak()方法。

SetAutoPageBreak(boolean auto [, float margin])

这里的第二个参数是定义触发限制的页面底部的距离。默认情况下,模式打开,边距为 2 厘米。

你已经设置了$pdf-&gt;SetAutoPageBreak(false);,所以你的页面不能得到分页符。

请从脚本中删除这一行,你会得到换行符;

更多信息请查看here

http://www.fpdf.org/en/doc/

编辑

请在评论部分尝试下面的代码

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();

输出

注意:如果不想要空白框而不是从脚本中删除代码。

【讨论】:

  • 悬赏结束时间为 1 小时。过了那个时间,我还能提供同样的赏金吗?
  • 是的,你可以 24 小时
【解决方案2】:

首先,你的数据有问题,所以我已经更正了你的数据并解决了你的问题(See result):

<?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();
?>

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

防止每页特定数量的 cmets 的控制流大部分是正确的。但是声明和启动 $max_comments_per_page_ = 25; 有一个错字,必须是 $max_comments_per_page = 25;

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

最终,FPDF 的基本(又名强制)分页功能实际上正在发挥作用。问题在于您的数据和您对循环的选择。

【讨论】:

  • 非常感谢您的回复。我尝试使用相同的逻辑,但我的代码为 cmets 生成 10 页,每页增加一条评论。你能看到我哪里出错了。我已将代码添加到附加代码下的原始帖子中
  • @DCJones $j 变量必须以 1 开头并在 if 条件下重置为 1。在while循环中$j也必须有一个增量:$j++我已经更新了我的答案。
  • 嗨,我已经根据您的编辑更改了我的代码,但结果不正确。我添加了“cmets”数据结果的图像。很抱歉脖子疼,但你能明白为什么会这样。再次感谢您的宝贵时间。
  • @DCJones 您能否将$row_Comments 的值或来自数据库的任何数据添加到您的问题中。如果没有您的确切数据,测试会很痛苦。几个小时后我会再看一遍,我保证我会解决的。
  • 我添加了用于测试的数组的内容。我希望这会有所帮助。
【解决方案3】:

根据问题,它通常发生在php程序员身上。我认为FPDF How to force a page break 这个答案可能对你很有用。你应该尝试一下。上次我使用 fpdf 时,我用数据库创建了一个简单的 pdf。它工作正常。也像 FPDF 一样,您也可以使用 MPDF 来生成 pdf。尝试链接答案。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多