Configuring TCPDF with Header and Footer

Hi Experts,

How can I get TCPDF to have a header and footer on every page? More so I need the header to be in html and would take up about 10% of the page.

Thank you
APD TorontoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Swatantra BhargavaTechnical Specialist/DeveloperCommented:
First Create HTML of your page, after that create PDF of that HTML.
0
Ray PaseurCommented:
I'm not sure I understand the question. The TCPDF class has these methods "baked in" so you can just extend the class and provide your own methods, right?  In my work with TCPDF, I often have to override the methods, so I know it can be done.  Here is how I make them "go away" and if you provide your own class methods, you should be able to get the exact formatting you need.
// EXTEND THE TCPDF OBJECT SO WE CAN SUBSTITUTE OUR OWN METHODS
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC HEADER AND FOOTER
    public function Header() {}
    public function Footer() {}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Hi Ray,

How would you in that case use your extended class to put your name of the top of every page?

Furthermore, are you able to also add the date to the header only on page 1, above your name?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Ray PaseurCommented:
TCPDF has online man pages:
http://www.tcpdf.org/doc/code/classTCPDF.html

TCPDF has a lot of online examples:
http://www.tcpdf.org/examples.php

Expect that a lot of experimentation will be needed.
http://iconoun.com/demo/temp_adp_toronto.php

<?php // demo/temp_adp_toronto.php

/**
 * http://www.experts-exchange.com/questions/28693143/Configuring-TCPDF-with-Header-and-Footer.html#a40857620
 *
 * Demonstrate how to put an image into a TCPDF document
 * and write some text over part of the image
 *
 * See http://www.tcpdf.org/doc/code/annotated.html
 */
error_reporting(E_ALL);

// A DATE SETTING MAY BE REQUIRED - DEPENDS ON PHP INSTALLATION SETTINGS
date_default_timezone_set('America/Chicago');

// SYNTHESIZE THE PDF FILE URL AND FILE NAME
$pdf_file_link    = 'storage/tcpdf_example.pdf';
$pdf_file_name    = getcwd() . DIRECTORY_SEPARATOR . $pdf_file_link;

// LOAD THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT SO WE CAN SUBSTITUTE OUR OWN METHODS
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC FOOTER
    public function Footer() {}

    // ADD CUSTOM HEADER
    public function Header()
    {
        $this->SetFont('helvetica', 'B', 20);
        $this->Cell
        ( 0                // $w width
        , 15               // $h height
        , $this->headerText
        , 0                // border
        , 1                // move cursor to next line after
        , 'L'              // left-align text
        , FALSE            // no fill
        , ''               // link (none)
        , 0                // stretch disabled
        , FALSE            // ignore min height
        , 'T'              // cell align
        , 'C'              // text aligh
        )
        ;
    }
}

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);

// SET THE TEXT FOR THIS HEADER
$pdf->headerText = 'Anything you want ' . date('c');

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// NO CELL BORDER IS NEEDED
$bdr = 0;

// ADD AN IMAGE
$img = 'images/ray_padded.png';

// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SCALE - EXPERIMENT WITH IT
$pdf->setImageScale(1.53);

// GET THE IMAGE SETTINGS
$pdf->Image
( $img
, 0             // $x
, 30            // $y
, 0             // WIDTH
, 0             // HEIGHT
, 'PNG'         // TYPE
, '#'           // LINK URL
, 'T'           // SET POINTER TOP LEFT
, FALSE         // NO RESIZING
, 300           // DPI
, 'L'           // PALIGN
, FALSE         // ISMASK
, FALSE         // IMGMASK
, 0             // BORDER
, FALSE         // FIT TO BOX
, FALSE         // HIDDEN
, FALSE         // FIT ON PAGE
)
;

// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24            // $x,
, 30+24         // $y,
, 'This is Ray'
, FALSE         // $fstroke           = false,
, FALSE         // $fclip             = false,
, TRUE          // $ffill             = true,
, $bdr          // $border            = whatever,
, 2             // $ln                = 0, 2=PUT CURSOR BELOW
, ''            // $align             = '', DEFAULT LEFT
, FALSE         // $fill              = false, CELL BACKGROUND
, ''            // $link              = '', NOT A LINK
, 0             // $stretch           = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign            = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign            = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff            = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;


// CHANGE THE TEXT FOR THIS HEADER
$pdf->headerText = 'New headerText';

// ADD ANOTHER PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

// ADD SOME TEXT ON THIS PAGE
$my_text = 'This is the second page';
$pdf->Text
( 24            // $x,
, 30+24         // $y,
, $my_text
, FALSE         // $fstroke           = false,
, FALSE         // $fclip             = false,
, TRUE          // $ffill             = true,
, $bdr          // $border            = whatever,
, 2             // $ln                = 0, 2=PUT CURSOR BELOW
, ''            // $align             = '', DEFAULT LEFT
, FALSE         // $fill              = false, CELL BACKGROUND
, ''            // $link              = '', NOT A LINK
, 0             // $stretch           = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign            = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign            = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff            = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;


// WRITE THE PDF FILE TO THE SERVER
$pdf->Output($pdf_file_name, 'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo '<a target="my_PDF" href="' . $pdf_file_link . '"><strong>Print the PDF</strong></a>';

Open in new window

0
APD TorontoSoftware DeveloperAuthor Commented:
Hi Ray,

I tried running your code above on my machine, but my file structure is as follows.

http://localhost/test/pdf_header.php is your code
http://localhost/tcpdf/ is the tcpdf directory

with this said, I adjusted the beginning of your code as such.  This code works in my other tests.

<?php 
// SYNTHESIZE THE PDF FILE URL AND FILE NAME
$pdf_file_name    = 'test.pdf';

// LOAD THE TCPDF CLASS AND CONFIGURATION
$currentDIR = getcwd();
            chdir("../");
            
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

            //Change DIR back
            chdir($currentDIR);

Open in new window


However, I am getting


Warning: require_once(tcpdf/config/lang/eng.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\test\pdf_header.php on line 9

Fatal error: require_once(): Failed opening required 'tcpdf/config/lang/eng.php' (include_path='.;C:\php\pear') in C:\inetpub\wwwroot\test\pdf_header.php on line 9
0
Ray PaseurCommented:
I'm going to step away from this and your other TCPDF questions.  I just can't seem to help you.  Even when I give you tested and working code examples, we don't seem to be making progress.  Good luck with the project.
0
APD TorontoSoftware DeveloperAuthor Commented:
So, my issue is that I'm not using your code and environment to the dot? Would it be easier if I just give yo my client?

I'm sure they will be thrilled that all their financial reports have your picture on every page
0
Swatantra BhargavaTechnical Specialist/DeveloperCommented:
Dear APD_Toronto,

I am working for you to create sample PDF which may help you.
0
APD TorontoSoftware DeveloperAuthor Commented:
Thank you.
0
APD TorontoSoftware DeveloperAuthor Commented:
With some experimenting I came up with the following code, but I am not getting "page 1" on my header as per lines 18-21.  This is my main objective here.

<?php
//Temporarily Change directory to TCPDF to load files...
    $currentDIR = getcwd();
    chdir("../tcpdf/examples");
    require_once('tcpdf_include.php');

    //Change DIR back
    chdir($currentDIR);

class MYPDF extends TCPDF {

	//Page header
	public function Header() {
		// Title
                $this->Ln();
                
                $page = $this->getAliasNumPage();
                if ($page == 1){
                    $this->Cell(0, 15, 'Page 1', 0, false, 'C', 0, '', 0, false, 'M', 'M');
                    $this->Ln();
                }
		$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
	}

	// Page footer
	public function Footer() {
		// Position at 15 mm from bottom
		$this->SetY(-15);
		// Set font
		$this->SetFont('helvetica', 'I', 8);
		// Page number
		$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
	}
}

    $pdf = new MYPDF('L', 'mm', array(215.9, 279.4));
        $pdf->SetTitle('Manifest');
        $pdf->SetMargins(5, 5);
        $pdf->SetAutoPageBreak(TRUE);
        $pdf->SetPrintHeader(TRUE);
        $pdf->SetHeaderMargin(10);
            $pdf->SetFont('Helvetica', '', 9);
            $pdf->AddPage();
            $pdf->AddPage();
            $pdf->AddPage();
            
             $file = 'Manifest.pdf';
      $pdf->Output($file); 
?>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.