2012-07-30 115 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调整,我认为:)