最后一页上的TCPDF更改页脚

时间:2012-07-30 09:10:49

标签: php tcpdf

我正在尝试使用TCPDF创建一个pdf,并在最后一页需要一个不同的页脚

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

我已经看过几篇有关此内容的文章,但无法使其发挥作用

非常感谢任何实施此功能的帮助

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页 - FIRST13 第2页 - NORMAL23 第3页(最后一页)NORMAL23

答案:

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;
}    

3 个答案:

答案 0 :(得分:12)

你自己的回答并没有回答你的问题,在最后一页有不同的页脚 我找到了following code from the author of tcPDF himself,它完全符合您的要求。

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) ...
    }
  }
}

答案 1 :(得分:1)

我希望这会有所帮助:

    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');
    }

OR

define("titulos_pie_pdf", ",ORIGINAL, COPY, TITLE1, TITLE2, ...", true);    

然后

    $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');

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

答案 2 :(得分:1)

嗨,我有类似的问题,这解决了它:

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;
}

希望它能帮到你:) 它不完全是你想要的,但我认为它的easyli可调:)

相关问题