mjsolo
mjsolo

Reputation: 315

TCPDF Change Footer on last page

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

Upvotes: 4

Views: 16162

Answers (4)

Francisco Suárez
Francisco Suárez

Reputation: 11

Check this

if (!$pdf->getAutoPageBreak()) {
        // is last page
}

Upvotes: 1

Peto Kovi Kováč
Peto Kovi Kováč

Reputation: 51

hi i had similar problem and this solved it:

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

hope it will help you :) its not exactly what you wanted but its easyli adjustable i think :)

Upvotes: 4

BX16Soupapes
BX16Soupapes

Reputation: 49

i hope this helps:

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

then

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

i´m not a big programmer, but this code helps me.

Upvotes: 1

Kim
Kim

Reputation: 2787

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

Upvotes: 16

Related Questions