Php TCPDF更改最后一页的页脚

Php TCPDF更改最后一页的页脚,php,tcpdf,Php,Tcpdf,我试图创建一个pdf使用TCPDF和需要一个不同的页脚上的最后一页 使用下面的代码,我可以在第一页获得不同的页脚,但不能在最后一页 我已经看了好几篇关于这个的帖子,但都没能成功 如果您有任何帮助,我们将不胜感激 public function Footer() { $tpages = $this->getAliasNbPages(); $pages = $this->getPage(); $footer = 'NORMAL' . $pages . $tpag

我试图创建一个pdf使用TCPDF和需要一个不同的页脚上的最后一页

使用下面的代码,我可以在第一页获得不同的页脚,但不能在最后一页

我已经看了好几篇关于这个的帖子,但都没能成功

如果您有任何帮助,我们将不胜感激

public function Footer() {
    $tpages = $this->getAliasNbPages();
    $pages = $this->getPage();

    $footer = 'NORMAL' . $pages . $tpages;

    if ($pages == 1 ) $footer = 'FIRST' . $pages . $tpages;
    if ($pages == $tpages) $footer = 'LAST' . $pages . $tpages;

    $this->Cell(0, 10, $footer, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
这给了我

第1页-第一页13 第2页-第23页 第3页(最后一页)23

答复:

public function Footer() {
    $tpages = $this->getAliasNbPages();
    $pages = $this->getPage();

    $footer = 'NORMAL' . $pages . $tpages;

    if ($pages == 1 ) $footer = 'FIRST' . $pages . $tpages;
    if ($this->end == true) $footer = 'LAST' . $pages . $tpages;

    $this->Cell(0, 10, $footer, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}

function display() {
    #function that has main text
    $this->AddPage();
    $html = '1st page';
    $this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
     $this->AddPage();
    $html = '2nd page';
    $this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

     $this->AddPage();
    $html = 'Last page';
    $this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);   
  $this->end = true;
}    

您自己的答案不能回答您在最后一页有不同页脚的问题。
我找到了,这正是你想要的

class mypdf extends tcpdf {

  protected $last_page_flag = false;

  public function Close() {
    $this->last_page_flag = true;
    parent::Close();
  }

  public function Footer() {
    if ($this->last_page_flag) {
      // ... footer for the last page ...
    } else {
      // ... footer for the normal page ...
    }
  }
}
现在,代码可以工作了,但前提是最后一页内容不同。在我的例子中,我可以有0-X最后的页面,所以我仍然需要依赖页面计数器。此代码适用于我:

class mypdf extends tcpdf {

  public $page_counter = 1;

  public function Make() {
    ...

    // Create your own method for determining how many pages you got, excluding last pages
    $this->page_counter = NUMBER;

    ...
  }

  public function Footer() {
    if ($this->getPage() <= $this->page_counter) {
      // ... footer for the normal page ...
    } else {
      // ... footer for the last page(s) ...
    }
  }
}
classmypdf扩展了tcpdf{
公共$page_计数器=1;
公共职能{
...
//创建自己的方法来确定获得的页面数,不包括最后一页
$this->page_counter=编号;
...
}
公共函数页脚(){
如果($this->getPage()页\计数器){
//…正常页面的页脚。。。
}否则{
//…最后一页的页脚。。。
}
}
}
我希望这有助于:

    if($this->page == 1){
        $this->Cell(0, 10, "ORIGINAL - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    } else {
        $this->Cell(0, 10, "COPY - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }

然后


我不是一个大程序员,但这段代码帮助了我。

嗨,我也遇到过类似的问题,这就解决了它:

public $isLastPage = false;

public function Footer()
{
    if($this->isLastPage) {    
      $this->writeHTML($this->footer);
    }
}

public function lastPage($resetmargins=false) {
    $this->setPage($this->getNumPages(), $resetmargins);
    $this->isLastPage = true;
}
希望它能帮助你:)
这并不完全是你想要的,但它很容易调整,我想:)

你找到解决方案了吗?我还面临着在调用output()之前获取总页数的问题。现在我正在pdf类中使用一个内部计数器。我刚刚解决了如何使其工作-我在生成正文的例程末尾添加了一个变量-页脚检查此变量是否已设置,如果已打印,页脚是否可以更新您的问题以添加为您修复它的代码?回答已添加到问题you节省了我很多时间。谢谢你,+1
    $titulos = explode(",",titulos_pie_pdf);
    $this->Cell(0, 10, $titulos[$this->page]." - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
public $isLastPage = false;

public function Footer()
{
    if($this->isLastPage) {    
      $this->writeHTML($this->footer);
    }
}

public function lastPage($resetmargins=false) {
    $this->setPage($this->getNumPages(), $resetmargins);
    $this->isLastPage = true;
}