Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF Change Footer on last page

Tags:

php

tcpdf

I am trying to create a pdf using TCPDF and need a different footer on the last page

using the following code I can get a different footer on the first page but not the last

I have looked at several post about this but can not make it work

Any help implementing this would be much appreciated

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

This gives me

page1 - FIRST13 page 2 - NORMAL23 page 3 (Last Page) NORMAL23

Answer:

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;
}    
like image 506
mjsolo Avatar asked Dec 02 '22 21:12

mjsolo


1 Answers

Your own answer does not answer your question to have a different footer on last page.
I found the following code from the author of tcPDF himself, which does exactly what you want.

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

Now that code works, but only if your last page differ. In my case I could have 0-X last pages, so I still need to rely on a page counter. This code works for me:

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) ...
    }
  }
}
like image 174
Kim Avatar answered Dec 27 '22 07:12

Kim