4

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

3 に答える 3

15

あなた自身の答えは、最後のページに別のフッターを持つという質問には答えません。
は tcPDF の作者自身から次のコードを見つけました。

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) ...
    }
  }
}
于 2012-08-22T08:59:49.533 に答える
2

こんにちは、同様の問題があり、これで解決しました:

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

それがあなたを助けることを願っています:)それはまさにあなたが望んでいたものではありませんが、簡単に調整できると思います:)

于 2015-03-30T14:49:00.260 に答える