【问题标题】:FPDF after page break using SetAutoPageBreak multicell values are not inserted in the defined Y position使用 SetAutoPageBreak 多单元格值分页后的 FPDF 未插入到定义的 Y 位置
【发布时间】:2019-09-26 13:56:51
【问题描述】:

我正在尝试使用 multiCell 显示数据。而当页面中的数据到达 y=228 时,我希望它转到下一个 Page 并显示在 y=112 的位置。

作为第一步,我尝试只添加 2 个简单条件:

当数据到达位置 y=228 时创建一个新页面 当数据转到下一页时,在位置 =112 处显示结果

成功了。但是如果当前的 multiCell 内容很大,它不会转到下一页,直到它完成写入所有 multicell 内容,所以我添加了函数 SetAutoPageBreak 所以它在 y=228 时插入一个分页符。 问题从这里开始代码没有将我的数据插入新页面中我定义的位置(y = 112)它插入到开始。我不知道如何解决这个问题我希望我能找到一些帮助我会很感激的。

这是我的代码:

<?php
require ('../fpdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
$y = $pdf->GetY();
$x = $pdf->GetX();
$width_cell = array(
    10,
    30,
    50
);
$pdf->SetFillColor(193, 229, 252);
$pdf->SetY(112);

$pdf->Cell($width_cell[0], 10, 'ID', 1, 0, C, true);
$pdf->Cell($width_cell[1], 10, 'NAME', 1, 0, C, true);
$pdf->Cell($width_cell[2], 10, 'CLASS', 1, 1, C, true);

$pdf->SetFont('Arial', '', 10);

for ($i = 0;$i < 30; $i++) {
    $pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
    $pdf->Cell($width_cell[1], 10, 'John Deo', 1, 0, C, false);
    $pdf->Cell($width_cell[2], 10, 'Four', 1, 1, C, false);
    $y = $pdf->GetY();
    $pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
    $pdf->Cell($width_cell[1], 10, 'Y:' . $y, 1, 0, C, false);

    // $pdf->Cell($width_cell[2],10,'Four',1, 1, C, false);
    $pdf->MultiCell($width_cell[2], 10, 'four four four four four four four four four four four four four four four four four four four four four four ', 1, C, false);

    // Uncomment this line to see what Happends when the Page Break is inserted
    // $pdf->SetAutoPageBreak(auto,69);
    $y = $pdf->GetY();
    if ($y > 228 && $i != 29) {
        $pdf->AddPage();
        $pdf->SetY(112);
    }

    /*
    if ($pdf->PageNo() != 1 && $y < 20){


       $pdf->SetY(112);
    }
    */
}
$pdf->Output();

?>

【问题讨论】:

    标签: php pdf fpdf


    【解决方案1】:

    扩展 fPDF 并添加一个标题函数,该函数将 Y 定位在每次启动新页面时所需的位置。

    require ('fpdf.php');
    class PDF extends FPDF {
    
        function Header() {
    
            $this->SetY(112);
        }
    
    }  // end of the PDF class
    
    $pdf = new pdf();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',12);
    $y=$pdf->GetY();
    $x=$pdf->GetX();
    $width_cell=array(10,30,50);
    $pdf->SetFillColor(193,229,252);
    $pdf->SetY(112);
    

    您的其余代码都放在这里,您确实想取消注释 AutoPageBreak 行。您还需要将 AutoPageBreak 行更改为阅读

    $pdf->SetAutoPageBreak(1,69);
    

    因为第一个参数是一个布尔值,表示是否应该启用它。

    【讨论】:

    • 非常感谢!这似乎是一个简单的解决方案,但我无法想到它。我非常感谢你救了我的命。感谢您的帮助,先生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多