dtkago3208 2016-08-10 22:33
浏览 116
已采纳

FPDF PHP - Line在第二页中无法正常工作

Following is my code which prints "HELLO", then a dotted line. This thing gets repeated 50 times. Everything is working fine but when 2nd page starts, dotted lines disappear. What modification is required in this code?

   <?php

    require("fpdf.php");

    class PDF extends FPDF
    {   
        function SetDash($black=null, $white=null)
        {
            if($black!==null)
                $s=sprintf('[%.3F %.3F] 0 d',$black*$this->k,$white*$this->k);
            else
                $s='[] 0 d';
            $this->_out($s);
        }
    }

    $pdf = new PDF('P', 'mm', 'A4');
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $margin = 0;

    $pdf->SetFont('Arial','B',12);

    for ($i = 0; $i < 50; $i++)
    {
        $pdf->Cell(90, 10, "Hello", 0, 1);
        $pdf->SetDrawColor(0,0,0);
        $pdf->SetDash(2,2); 
        $margin = $margin + 10;
        $pdf->Line(10,$margin,200,$margin);
    }

    $pdf->Output();

    ?>

展开全部

  • 写回答

1条回答 默认 最新

  • dpxkkhu1812 2016-08-11 01:00
    关注

    You're incrementing the value of your $margin variable by 10 after each line even if a page break occurs in the middle of the loop. Thus, the top margin of the first line on the second page will be 10 millimeters greater than the top margin of the last line on the first page.

    You need to reset the margin when a new page is added.

    A solution for this problem would be to override FPDF's AcceptPageBreak method. This method intercepts the adding of a new page when the bottom of a page is reached.

    class PDF extends FPDF
    {
        var $lineY = 0;
    
        // ...
    
        function AcceptPageBreak()
        {
            $this->lineY = 0;
            return parent::AcceptPageBreak();
        }
    }
    

    Then, in your loop, you can do:

    $pdf->Line(10, $pdf->lineY, 200, $pdf->lineY);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部