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

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。