Solved

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

Posted on 2011-03-14
2
10,895 Views
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:

http://www.tcpdf.org/examples/example_003.phps

(from http://www.tcpdf.org/examples.php)

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
        $this->SetY(-15);
        // 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?

0
Comment
Question by:freestate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
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() 
	{			
		switch($this->page)
		{
				case 1: 
					$this->Cell(0, 0 "Header 1", 0, 1, 'C'); 
					break;
				case 2: 
					$this->Cell(0, 0 "Header 2", 0, 1, 'C'); 
					break;
		}
        }

		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->SetLineWidth($line_width);
				$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) {
					$this->Ln();
					$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}';
				
				$this->SetY($footer_y); 
				
				//Print page number
				if ($this->rtl) {
					$this->SetX($this->original_rMargin);
					$this->Cell(0, $footer_height, $pagenumtxt, 'T', 0, 'L');
				} else {
					$this->SetX($this->original_lMargin);
					$this->Cell(0, $footer_height, $pagenumtxt, 'T', 0, 'R');
				}
				// restore line width
				$this->SetLineWidth($prevlinewidth);
				
				// restore font values
				$this->SetFont($font_family, $font_style, $font_size);
			}
		}


}

Open in new window

0
 

Author Comment

by:freestate
ID: 35134141
Perfect - exactly what I needed!

Thanks a lot!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

728 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