How do I edit a footer using tcpdf? I want to add current date & time at the footer. Please help.
Asked
Active
Viewed 3.9k times
7 Answers
21
Override the class like this :
class MYPDF extends TCPDF {
public function Footer() {
$image_file = "img/bg_bottom_releve.jpg";
$this->Image($image_file, 11, 241, 189, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
$this->SetY(-15);
$this->SetFont('helvetica', 'N', 6);
$this->Cell(0, 5, date("m/d/Y H\hi:s"), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
then instead of calling :
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
do :
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

Oli
- 1,622
- 18
- 14
-
By using this code date shown in the middle but I want to show the date in the left corner what should I do? – Krunal Pandya Mar 31 '20 at 12:23
6
You have to extend the TCPDF class and override the Footer() method as explained on the default example n. 3. Check the official http://www.tcpdf.org website and forums for further information.

user412934
- 364
- 4
- 5
3
How can I create 2 footer lines, please?
class MYPDF extends TCPDF {
private $customFooterText = "";
/**
* @param string $customFooterText
*/
public function setCustomFooterText($customFooterText)
{
$this->customFooterText = $customFooterText;
}
public function Footer()
{
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, $this->customFooterText, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Usage
$pdf = new MYPDF();
$pdf->setCustomFooterText('THIS IS CUSTOM FOOTER');

Samuel Philipp
- 10,631
- 12
- 36
- 56
-
For multiple lines of text I think it's easier to use HTML, defining it in $mycustomfootertxt: Replace $this->Cell line with $this->writeHTML($mycustomfootertxt, false, true, false, true); – Jimbo Jun 09 '21 at 21:49
1
If you want dynamically change footer text extends TCPDF like this
class MYPDF extends TCPDF {
private $customFooterText = "";
/**
* @param string $customFooterText
*/
public function setCustomFooterText($customFooterText)
{
$this->customFooterText = $customFooterText;
}
public function Footer()
{
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, $this->customFooterText, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
usage:
$pdf = new MYPDF();
$pdf->setCustomFooterText('THIS IS CUSTOM FOOTER');
// .... etc

Bamz3r
- 177
- 2
- 7
0
If you want put an different content on more than one page:
define("bottom_info", "|FIRST PAGE|SECOND PAGE|THIRD PAGE|...", true);
the info will be separated by " | " (with explode function)
class:
class MYPDF extends TCPDF {
public function Header() {
}
// Page footer
public function Footer() {
$this->SetY(-15);
$this->SetFont('helvetica', '', 7);
// Page number
$titulos = explode("|",bottom_info);
$this->Cell(0, 10, $titulos[$this->page].' - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}

BX16Soupapes
- 39
- 2
-1
After the EOD use the below code to overwrite the footer method.
EOD;
$txt = date("m/d/Y h:m:s");
// print a block of text using Write()
$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);

ChuckO
- 2,543
- 6
- 34
- 41
-1
I figured it out and here's the solution for others that might come across this thread.
Edit the file "tcpdf.php".
Search for "public function Footer()
"
Add timestamp here:
$timestamp = date("m/d/Y h:m:s");
if (empty($this->pagegroups)) {
$pagenumtxt = ' Created on ' .$timestamp.' '.$this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
} else {
$pagenumtxt = ' Created on ' .$timestamp.' '. $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
}

ire_and_curses
- 68,372
- 23
- 116
- 141

lv949
- 35
-
This got voted down because it's better to extend the class and override the method than to edit the source as suggested in this answer. HTH – TheDavidFactor Oct 19 '15 at 02:56
-
Btw: in many cases you can't edit dependencies like TCPDF because otherwise you have to add it to your repository – Spears Apr 23 '18 at 08:09