【问题标题】:FPDI, FPDF SetAutoPageBreak add template to page after breakFPDI、FPDF SetAutoPageBreak 在中断后将模板添加到页面
【发布时间】:2014-09-02 07:34:04
【问题描述】:

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

谢谢

【问题讨论】:

    标签: php fpdf fpdi


    【解决方案1】:

    只需将导入的页面放在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】:

      除了@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 正在寻找的东西,但也许它可以帮助其他人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-26
        • 2012-09-03
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2016-05-02
        相关资源
        最近更新 更多