ladieu
ladieu

Reputation: 115

TCPDF - weird issue with footers

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.

Upvotes: 0

Views: 4109

Answers (2)

ladieu
ladieu

Reputation: 115

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

Upvotes: 2

Mark Storer
Mark Storer

Reputation: 15870

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.

Upvotes: 1

Related Questions