FPDI、FPDF SetAutoPageBreak在中断后向页面添加模板



我创建了一个表单,允许用户创建一个页面数量不受限制的pdf,我设置了SetAutoPageBreak,以便它继续到第二个页面,但我无法让在页面中断后创建的页面继续使用原始模板文件。基本代码如下所示。

require('fpdf.php');
require('fpdi.php');
$pdf = new FPDI('P','mm','A4');
    $pageCount = $pdf->setSourceFile("source_file.pdf");
    $tplIdx = $pdf->importPage(1);
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx);
    $pdf->SetTextColor(63,76,89);
    $pdf->SetMargins(5,39,5,20);
    $pdf->SetAutoPageBreak(true,22); //page created doesn't have template attached
    $pdf->SetDrawColor(225,225,225);
    $pdf->SetFillColor(248,248,248);
    $pdf->SetLineWidth(1);
    $pdf->SetXY(82, 40);
    $pdf->MultiCell(165,5,$company.$block,0,L,false);
    $pdf->SetXY(19, 45);
    $pdf->MultiCell(165,5,$date.$block,0,L,false);
    $pdf->Output();

环顾四周,这个问题是我能找到的最接近的问题,但我不确定它是否相关:FPDF/FPDI UseTemplate

感谢

只需将导入的页面放在Header方法中即可:

class PDF extends FPDI
{
    protected $_tplIdx;
    public function Header()
    {
        if (null === $this->_tplIdx) {
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);
    }
}
$pdf = new PDF('P','mm','A4');
$pdf->AddPage();
...

一切都应该按预期进行。

除了@JanSlabon的回答:(我没有写评论所需的声誉,所以我会在这里发布,希望没关系)

如果你只想在第一页使用某个模板,在所有其他页面使用不同的模板,你可以这样做:

class PDF extends FPDI
{
    protected $_tplIdx;
    public function Header()
    {
        if (null === $this->_tplIdx) {
            $this->setSourceFile('paper1.pdf');
            $this->_tplIdx = $this->importPage(1);
        } else {
          $this->setSourceFile('paper2.pdf');
          $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);
    }
}

我知道这并不完全是@Searlee想要的,但也许它能帮助其他人。

最新更新