TCPDF - 页脚的奇怪问题

发布于 2024-10-17 09:30:19 字数 356 浏览 3 评论 0原文

在此处查看源代码:

http://www.savvissl.com/demo1/showcode.php< /a>

在此处查看脚本

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

问题是……除了最后一页之外,每页的页脚打印效果都很好。最后一页从来没有页脚。如果文档中只有一页,则根本不会打印页脚。

Check out the source code here:

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

check out the script here

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

Here is the issue... the footer prints fine on every page except for the last page. The last page never has a footer. If there is only one page in the document the footer will not print at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛刀软妹 2024-10-24 09:30:19

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

<?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 示例文件夹中的示例表现出相同的问题,但是如果您从他们的网站运行它,页脚显示正确。无论如何希望这对某人有帮助

OK I couldn't figure it out, but i was able to copy a co-workers example that worked. If anyone wants the source code here it is:

<?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}");
?>

Comparing this new code to my old reveals no insight into why this works... in fact the example in the TCPDF examples folder exhibits the same issue, however if you run it from their website the footer is displayed correctly. Well anyway hope this helps someone

橘味果▽酱 2024-10-24 09:30:19

除了我刚刚通过浏览他们的文档了解到的内容之外,我对 TCPDF 一无所知。

看起来只有当您显式调用 AddPage() 时才会调用 Footer() ,此时它会添加到上一页。其余时间我相信你必须自己调用它。

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

您可能想要:“起始页、页眉、绘制文本、页脚、结束页”。看起来 Write() 为您调用了 AddPage(),这就是为什么页眉和页脚出现在除最后一页之外的所有页面上。

底线:在此示例中,只需在调用 Write() 后调用 Footer() 即可。现实世界的例子几乎肯定会更复杂一些。

I know nothing about TCPDF save what I just learned going through their docs.

It looks like Footer() is only called for you when you explicitly call AddPage(), at which point it is added to the PREVIOUS PAGE. The rest of the time I believe you have to call it yourself.

There's also this whole StartPage()/EndPage() thing that sounds like an alternative to AddPage().

You might want to: "start page, header, draw text, footer, end page" instead. It looks like Write() calls AddPage() for you, which is why the headers and footers on all-but-the-last-page are present.

Bottom Line: Just call Footer() after you call Write() in this example. Real world examples will almost certainly be a bit more complex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文