【问题标题】:TCPDF - weird issue with footersTCPDF - 页脚的奇怪问题
【发布时间】:2011-02-10 22:44:49
【问题描述】:

在此处查看源代码:

http://www.savvissl.com/demo1/showcode.php

在这里查看脚本

http://www.savvissl.com/demo1/testPDF.php

这是问题所在……除了最后一页之外,页脚在每一页上都打印得很好。最后一页永远没有页脚。如果文档中只有一页,则根本不会打印页脚。

【问题讨论】:

  • 他做到了...在第一个链接中。

标签: php pdf tcpdf


【解决方案1】:

好的,我想不通,但我能够复制一个有效的同事示例。如果有人想要这里的源代码,那就是:

<?php

require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');


define('PDF_FOOTER_TEXT','800 Vinial St. Pittsburgh, PA 15212 | phone: 412.321.7006 | fax: 412.321.7005 | www.savvior.com');
$PDF_LINE_COLOR=array(255,255,0);
define('PDF_FOOTER_TEXT_COLOR',170);


class MYPDF extends TCPDF
{
    //Page header
    public function Header()
    {
        global $PDF_LINE_COLOR;
        $image_file = K_PATH_IMAGES.'image.jpg';
        $this->Image($image_file, 160, 0, 30, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
        $this->SetFont('helvetica', 'B', 20);
        $this->Cell(0, 15, '', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->line(10,27,200,27,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'solid' => 4, 'color' => $PDF_LINE_COLOR));
    }
    public function Footer()
    {
        global $PDF_LINE_COLOR;
        $cur_y = $this->GetY();
        $ormargins = $this->getOriginalMargins();
        $this->SetTextColor(PDF_FOOTER_TEXT_COLOR, PDF_FOOTER_TEXT_COLOR, PDF_FOOTER_TEXT_COLOR);
        $this->SetY($cur_y);
        $this->line(10,400,200,400,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'solid' => 4, 'color' => $PDF_LINE_COLOR));
        $this->Cell(0,11,"Page ". $this->getAliasNumPage().'/'.$this->getAliasNbPages(),'T',0,'L');
        $this->Cell(0,11,PDF_FOOTER_TEXT,'T',0,'R');
    }

}

ob_start();
    ?><h1>Content Is Needed For This Page...</h1>
    ...
    <?

    $html=ob_get_clean();

function makePDFFile($fileName,$html)
{
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Savvior Project Manager');
    $pdf->SetTitle('Auto Generated PDF');
    $pdf->SetSubject('Auto Generated PDF');
    $pdf->SetKeywords('TCPDF');
    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP+5, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    //set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    //set some language-dependent strings
    $pdf->setLanguageArray($l);
    // set font
    $pdf->SetFont('helvetica', '', 12);
    // add a page
    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, true, false, '');
    $doc=$pdf->Output(dirname(__FILE__)."/cache/{$fileName}", 'F');
    return $fileName;
}

$file=makePDFFile('poo-poo-platter.pdf',$html);

header("location: cache/{$file}");
?>

将这个新代码与我的旧代码进行比较并没有发现为什么它会起作用……事实上,TCPDF 示例文件夹中的示例也存在同样的问题,但是如果您从他们的网站运行它,页脚将正确显示。无论如何,希望这可以帮助某人

【讨论】:

    【解决方案2】:

    除了我刚刚学到的going through their docs,我对 TCPDF 一无所知。

    看起来Footer() 仅在您显式调用AddPage() 时为您调用,此时它被添加到上一页。剩下的时间我相信你必须自己打电话。

    还有整个StartPage()/EndPage() 听起来像是AddPage() 的替代品。

    您可能想要:“开始页、页眉、绘制文本、页脚、结束页”。看起来Write() 为您调用了AddPage(),这就是所有-but-the-last-page 上的页眉和页脚都存在的原因。

    底线:在本例中,在调用Write() 之后调用Footer()。现实世界的例子几乎肯定会更复杂一些。

    【讨论】:

    • 谢谢老兄,一直没弄明白,但重新开始就解决了……见下文
    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2014-02-10
    相关资源
    最近更新 更多