TCPDF: How to have different headers/footers on different pages?

Posted on 2011-03-14
Last Modified: 2013-11-13
I'm using TCPDF to generate PDF documents on the fly from a database.

I need to use custom headers:


BUT, have hit two problems:

1. I need to suppress the footer on page one, so as not to have the page number on the title page

2. I need to vary the text in the header on each page.

I've tried calling a new Footer before each new Addpage:

class MYPDF extends TCPDF {

    // Page footer
    public function Footer() {

      if (!isset($i)) $i = 1;
      else {
        // Position at 15 mm from bottom
        // Set font
  $this->Cell(0, 0, '', T, 1, 'L', 0, '');
       $this->SetFont('helvetica', 'B', 12);
        // Title
        $this->Cell(30, 0, 'Design Out Crime ', 0, 0, 'L', 0, '');
       $this->SetFont('helvetica', 12);
        // Title
        $this->Cell(100, 0, 'Your Personalised Advice', 0, 0, 'L', 0, '');
       $this->SetFont('helvetica', 'B', 12);
       // Page number
        $this->Cell(0, 0, ''.$this->getAliasNumPage().'', 0, 0, 'R', 0, '');

But it only seems to take the first footer defined.

Can anybody help?

Question by:freestate

Accepted Solution

wmadrid1 earned 500 total points
ID: 35134118
I already did someting as it.
You can extend the TCPDF class
use $this->page propertie to controls header and footer,
and override header and footer methods

class MYPDF extends TCPDF 
	function Header() 
				case 1: 
					$this->Cell(0, 0 "Header 1", 0, 1, 'C'); 
				case 2: 
					$this->Cell(0, 0 "Header 2", 0, 1, 'C'); 

		public function Footer() {
			if ($this->print_footer && $this->page>1) {
				if (!isset($this->original_lMargin)) {
					$this->original_lMargin = $this->lMargin;
				if (!isset($this->original_rMargin)) {
					$this->original_rMargin = $this->rMargin;
				// reset original header margins
				$this->rMargin = $this->original_rMargin;
				$this->lMargin = $this->original_lMargin;
				// save current font values
				$font_family =  $this->FontFamily;
				$font_style = $this->FontStyle;
				$font_size = $this->FontSizePt;
				$this->SetTextColor(0, 0, 0, false);
				//set font
				$this->SetFont($this->footer_font[0], $this->footer_font[1] , $this->footer_font[2]);
				//set style for cell border
				$prevlinewidth = $this->GetLineWidth();
				$line_width = 0.3;
				$this->SetDrawColor(0, 0, 0);
				$footer_height = round((K_CELL_HEIGHT_RATIO * $this->footer_font[2]) / $this->k, 2); //footer height
				//get footer y position
				$footer_y = $this->h - $this->footer_margin - $footer_height;
				//set current position
				if ($this->rtl) {
					$this->SetXY($this->original_rMargin, $footer_y);
				} else {
					$this->SetXY($this->original_lMargin, $footer_y);
				//print document barcode
				if ($this->barcode) {
					$barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin)/3); //max width
					$this->writeBarcode($this->GetX(), $footer_y + $line_width, $barcode_width, $footer_height - $line_width, "C128B", false, false, 2, $this->barcode);
				$pagenumtxt = $this->l['w_page']." ".$this->PageNo().' / {nb}';
				//Print page number
				if ($this->rtl) {
					$this->Cell(0, $footer_height, $pagenumtxt, 'T', 0, 'L');
				} else {
					$this->Cell(0, $footer_height, $pagenumtxt, 'T', 0, 'R');
				// restore line width
				// restore font values
				$this->SetFont($font_family, $font_style, $font_size);


Open in new window


Author Comment

ID: 35134141
Perfect - exactly what I needed!

Thanks a lot!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 86
How to use md5 hashing 3 29
how to use a function in heredoc 5 23
Scope of $_SESSION 17 30
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now