Cell使用FPDF PHP中的SetXY()在新页面中启动

时间:2016-10-04 15:53:52

标签: php fpdf

我正在尝试生成这种pdf,在第一页中一切正常。

enter image description here

但是,当我循环次数超过23次时,看到Cell()在分页时在新页面中开始。

enter image description here

我希望这个Cell()像第一页一样显示。

这是我的代码:

class PDF extends FPDF {
    public $hPosX;
    public $hPosY;
    function FancyTable() {
        $this->SetFillColor(255,0,0);
        $this->SetTextColor(255,0,0);
        $this->SetDrawColor(128,0,0);
        $this->SetLineWidth(.3);
        $this->SetFont('','B');

        $this->headerTable();
     }

    public function headerTable(){
        $this->hPosX = 15;
        $this->hPosY = 3;
        $this->SetFillColor(255,0,0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128,0,0);
        $this->SetLineWidth(.3);
        $this->SetFont('','B');
        for($i=0; $i<30;$i++){
            if($this->GetX() > 160){
                $this->hPosX  = 15;
                $this->hPosY  += 35; 
            }

            $this->SetXY($this->hPosX,$this->hPosY);
            $this->Cell(50,5,'Header'.$i,1,0,'L',true); 

            $this->hPosX += 55;
        }
    }
}




$pdf = new PDF();
$pdf->SetTopMargin(10);
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->FancyTable();
$pdf->Output();

任何解决方案都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

解决。如果GetY()大于当前页面,添加新页面并将hPosY变量重置为其默认值,我必须停止分页。

我还检查了GetX()是否大于160并将hPosX重置为其默认值,因此页面上的最后一个元素将适合页面并在新页面上正确启动。

以下是已修改方法的代码&#39; headerTable()&#39;:

public function headerTable(){
$this->hPosX = 15;
$this->hPosY = 3;
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
for($i=0; $i<30;$i++){
if($this->GetX() > 160){
$this->hPosX  = 15;
$this->hPosY  += 35; 
}

$this->SetXY($this->hPosX,$this->hPosY);
$this->Cell(50,5,'Header'.$i,1,0,'L',true); 

$this->hPosX += 55;

if($this->GetY() > 240 && $this->GetX() > 160){
$this->SetAutoPageBreak(false);
$this->AddPage();
$this->hPosX = 15;
$this->hPosY = 3;
}
}

}
相关问题