草庐IT

PHP FPDF 分页符不起作用

coder 2024-04-07 原文

我的拼贴项目即将结束,我现在正在尝试编写一个脚本,使用 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 [Comments] => 缺口 - 划痕 - 染色 - 需要油漆)数组([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID203 [Comments] =>房间门 Handlebars /防撞板 - 不安全/不工作 安全门链 - 不工作 房间门死锁 - 操作不正确不工作)阵列([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID404 [评论] => 门铰链 - 吱吱声/棍子 - 需要油/维修)阵列([UniqueID] => NXLHR01071474538755 [SeqID] => SeqID502 [评论] => 门 Handlebars /防撞板 - 不安全/不工作 ) Array ( [UniqueID] => NXLHR01071474538755 [SeqID] => SeqID1411 [Comments] => 水龙头 - 不安全/泄漏 - 修复弹出式塞子或插头 - 需要调整布线 - 损坏/破损 - 修复浴缸面板 - 损坏 - 修复开裂不良灌浆/密封剂/硅/条 - 修复淋浴喷头 - 起泡器de-Scaled ) Array ( [UniqueID] => NXLHR01071474538755 [SeqID] => SeqID1305 [Comments] => Stained/Discolored/Limescale Visible/Tarnished - Repair)

最佳答案

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

SetAutoPageBreak(boolean auto [, float margin])

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

你已经设置了$pdf->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();

输出

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

关于PHP FPDF 分页符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465168/

有关PHP FPDF 分页符不起作用的更多相关文章

  1. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  2. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  3. ruby - 每个页面上的 Jekyll 分页 - 2

    据我们所知,Jekyll默认分页仅支持index.html,我想创建blog.html并在那里包含分页。有什么解决办法吗? 最佳答案 如果您创建一个名为/blog的目录并在其中放置一个index.html文件,那么您可以向_config.yml表示paginate_path:"blog/page:num"。不是使用根文件夹中的默认index.html作为分页器模板,而是使用/blog/index.html。分页器将根据需要生成类似/blog/page2/和/blog/page3/的页面。这将使您到达yourwebsite.com/b

  4. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

  5. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  6. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

  7. ruby-on-rails - Simple_form 必填字段不起作用 - Ruby on Rails - 2

    我在RoR应用程序中有一个提交表单,是使用simple_form构建的。当字段为空白时,应用程序仍会继续下一步,而不会提示错误或警告。默认情况下,这些字段应该是required:true;但即使手动编写也行不通。该应用有3个步骤:NewPost(新View)->Preview(创建View)->Post。我的Controller和View的摘录会更清楚:defnew@post=Post.newenddefcreate@post=Post.new(params.require(:post).permit(:title,:category_id))ifparams[:previewButt

  8. ruby-on-rails - Heroku Action 缓存似乎不起作用 - 2

    我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错

  9. ruby-on-rails - Rake 预览在 Octopress 中不起作用 - 2

    我在我的机器上安装了ruby​​版本1.9.3,并且正在为我的个人网站开发一个octopress项目。我为我的gems使用了rvm,并遵循了octopress.org记录的所有步骤。但是我在我的rake服务器中发现了一些错误。这是我的命令日志。Tin-Aung-Linn:octopresstal$ruby--versionruby1.9.3p448(2013-06-27revision41675)[x86_64-darwin12.4.0]Tin-Aung-Linn:octopresstal$rakegenerate##GeneratingSitewithJekyllidenticals

  10. ruby - 比较运算符不起作用(在 erb View 中) - 2

    我是RubyonRails的新手,我正在尝试编写一个morethan表达式:5%>大于号不断抛出异常捕获错误。我不确定如何解决这个问题?编辑:这不是rails,也不是View,它是一个Ruby构造 最佳答案 使用5%>错误来自photo_limit而不是从Integer延伸类(猜测它真的是一个字符串),因此没有混合比较方法/s有关更多信息,请参阅:http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/特别是你必须混入Comparable并定义方法。虽然在这

随机推荐