代码之家  ›  专栏  ›  技术社区  ›  Searlee

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

  •  0
  • Searlee  · 技术社区  · 10 年前

    我已经创建了一个表单,允许用户创建具有无限页数的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

    谢谢

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jan Slabon    10 年前

    只需将导入的页面放在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();
    ...
    

    ……一切都应该按预期进行。

        2
  •  1
  •   skalta    4 年前

    除了@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在寻找的,但也许这对其他人有所帮助。