在 FPDF 中使用 SetXY() 在新页面中开始单元格 PHP

Cell Starts In The New Page using SetXY() in FPDF PHP

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

但是当我循环超过 23 次时,它看起来 Cell() 在分页时从新页面开始。

我希望这个 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();

任何解决方案将不胜感激。

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

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

这里是修改方法的代码'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;

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

}