I am trying to create a letter template in TCPDF but struggling with the footer.
I have added the below
$footertext="Registered Charity no XXX. Company Limited by Guarantee registered in England and Wales no XXX. <br>
Registered Office: ADDRESS HERE";
And then added the below in the PDF create section
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', '', 8);
// Page number
$this->writeHTML($footertext, false, true, false, true);
}
However nothing shows?
You have to create an instance of fPDF before creating your footer function.
require('fpdf.php');
class PDF extends FPDF {
//
// This class extends FPDF so we may customize the header and footer
// of the PDFs that are created
//
function Footer() {
$this->SetFont('helvetica', '', 8);
// Page number
$this->writeHTML($footertext, false, true, false, true);
} // end of the Footer function
} // end of the PDF class
Related
So I have run into a bit of a problem with fpdf. I am generating a report successfully, except when I try to add a header/footer, it just displays a blank page. Not even the Pdf viewer. I have just copied the example from the tutorial on the fpdf website, and I have used $pdf-> new PDF instead of $pdf-> new FPDF, as suggested under every similar question here. But the problem persists. There seems to be another issue, that is the I also get a blank page unless I comment out the require 'fpdf/fpdf.php';. Only if it is commented out, does the pdf show.
Here's my code :
//require '/usr/share/php/fpdf/fpdf.php';
/////////ADD HEADER, FOOTER AND NEW PAGE/ MARGIN SETTING////////////
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
$this->Image("../tmp/headers/header2.",0,0,210);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(30,10,'Title',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
//$pdf = new PDF();
//$pdf->AliasNbPages();
//$pdf->AddPage();
//$pdf->Header();
//$pdf->SetFont('Times','',12);
//for($i=1;$i<=40;$i++)
// $pdf->Cell(0,10,'Printing line number '.$i,0,1);
//$pdf->Output();
//-------------------- Start of Title Page --------------------//
//start pdf file, add page, set font
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddGBFont();
$pdf->AddBig5Font();
$pdf->AddPage();
/////rest of the report /////
$pdf->Output();
I'm using TCPDF in combination with FPDI. All working good except the Footer (want to add Page Numbers there).
My Code:
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends FPDI {
// 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, 5, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// add external PDF with FPDI
$pdf = new FPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(1, 4, 1, 1, true);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 3);
... this footer hook is working fine if I'm not suing FPDI. What do I miss here?
You are initiating an instance of FPDI and not of MYPDF. For sure the Footer() method will not be invoked that way.
IIRC you also have to leave setPrintFooter() to true.
I am using tFPDF, how can I override Footer and Header methods? I am initiating my pdf with this code:
$pdf = new tFPDF('P', 'mm', 'A4');
So this code it is not working for me
class PDF extends tFPDF
{
// Page header
function Header()
{
// Logo
$this->Image('logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(30,10,'Title',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
The example code is here: https://tcpdf.org/examples/example_003
From this you can easily see what goes wrong. You create a new PDF with:
$pdf = new tFPDF('P', 'mm', 'A4');
calling the original tFPDF class, not the class you created with your custom header and footer. You should use:
$pdf = new PDF('P', 'mm', 'A4');
Since PDF is the class with your header and footer in it.
Use the following code:
class MYPDF extends tFPDF
...
$pdf = new MYPDF('P', 'mm', 'A4');
I'm new to programming and I'm creating a pdf with FPDF(A PHP Class). At the bottom of my code you will notice that I create a new page and call the function Header(). For some reason, the header is also applied to the first page. How do I only apply the header to the second page?
Here is a link to a live version of the pdf: http://fosterinnovationculture.com/experiments/fpdf/index-two.php
<?php
require('fpdf.php');
// classes
class PDF extends FPDF
{
function Logo(){
$this->Image('images/logo.png',1,5.5,3);
}
//change name of function
function HeaderOne($xCor, $yCor, $text)
{
$this->SetX($xCor);
$this->SetY($yCor);
$this->SetFont('Arial','B',18);
$this->Cell(5,1,$text);
}
//Add bottom border
function BorderLine()
{
$this->SetDrawColor(0,0,0);
$this->SetFillColor(0,0,0);
$this->Rect(1, 10, 6.5, .015, 'F');
}
function Footer()
{
// Go to 1.5 cm from bottom
$this->SetY(-5.7);
// Select Arial italic 8
$this->SetFont('Arial','I',8);
// Print centered page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'R');
}
function Header(){
// Subtitle
$this->SetY(.25);
$this->SetX(1);
$this->SetFont('Arial','',12);
$this->Cell(6,1,'INNOVATION READINESS ASSESSMENT');
// Line
$this->SetDrawColor(0,0,0);
$this->SetFillColor(0,0,0);
$this->Rect(1, .5, 6.5, .015, 'F');
}
}
//pdf document preferences
$pdf = new PDF('p', 'in', 'Letter');
$pdf->AddPage();
$pdf->SetMargins(1,1,1,1);
//Page 1
$pdf->SetDrawColor(238,170,40);
$pdf->SetFillColor(238,170,40);
$pdf->Rect(1, 1, 6.5, .25, 'F');
$pdf->Image('images/header.jpg',1,1.5,6.5,3.5,'JPG','');
$pdf->Logo();
$pdf->HeaderOne(1,9,'Innovation Readiness Assessment');
$pdf->BorderLine();
//Page 2
$pdf->AddPage();
$pdf->Header();
// Close and output file to the browser
$pdf->Output();
?>
See the official documentation for Header() and
AddPage().
The AddPage() function already internally calls Header().
There is no need to call Header() explicitly here.
AddPage() in tcpdf automatically calls Header and Footer. How do I eliminate/override this?
Use the SetPrintHeader(false) and SetPrintFooter(false) methods before calling AddPage(). Like this:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
A nice easy way to have control over when to show the header - or bits of the header - is by extending the TCPDF class and creating your own header function like so:
class YourPDF extends TCPDF {
public function Header() {
if (count($this->pages) === 1) { // Do this only on the first page
$html .= '<p>Your header here</p>';
}
$this->writeHTML($html, true, false, false, false, '');
}
}
Naturally you can use this to return no content as well, if you'd prefer to have no header at all.
Here is an alternative way you can remove the Header and Footer:
// Remove the default header and footer
class PDF extends TCPDF {
public function Header() {
// No Header
}
public function Footer() {
// No Footer
}
}
$pdf = new PDF();
How do I eliminate/override this?
Also, Example 3 in the TCPDF docs shows how to override the header and footer with your own class.
Example:
- First page, no footer
- Second page, has footer, start with page no 1
Structure:
// First page
$pdf->startPageGroup();
$pdf->setPrintFooter(false);
$pdf->addPage();
// ... add page content here
$pdf->endPage();
// Second page
$pdf->startPageGroup();
$pdf->setPrintFooter(true);
$pdf->addPage();
// ... add page content here
$pdf->endPage();
// set default header data
$pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, 'marks', '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));
With the help of above functions you can change header and footer.