dtkago3208 2016-08-11 06: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 09: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测