2012-07-30 120 views
2
最後一頁

我試圖使用TCPDF來創建一個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頁 - 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; 
}  
+0

你找到一個解?我也遇到了調用總頁數_before_output()的問題。現在我在我的pdf課程中使用內部計數器。 – Kim 2012-08-17 14:19:24

+0

我剛剛制定瞭如何使它工作 - 我添加了一個變量,以生成正文的例程結束 - 頁腳檢查,如果這個變量設置,如果它打印結束頁腳 – mjsolo 2012-08-20 08:18:10

+0

你可以更新你的問題到添加修復它的代碼? – Kim 2012-08-21 11:28:26

回答

10

你自己的答案不回答你的問題有去年不同的頁腳頁。
我找到了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) ... 
    } 
    } 
} 
+0

您爲我節省了很多時間。謝謝+1 – 2014-01-18 08:59:21

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

我可不是一個很大的程序員,但此代碼可以幫助我。

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調整,我認爲:)