header and footer line in FPDI/TCPDF - php

I'm stamping my PDF documents with FPDI and TCPDF functions, and I'm trying to figure it out, how can I add a line below the text in Header, and upon the text in footer. Here is my code:
<?php
require_once('lib/tcpdf/tcpdf.php');
require_once('fpdi.php');
$fullPathToFile = "TestClanek6.pdf";
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile; //global
if(is_null($this->_tplIdx)) {
// number of pages
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
if($this->page > 0) {
//$this->SetPrintHeader(true);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0);
$this->Write(15, "Vol. 1, No. 15, Year: 2015, duff");
$this->Image('logo.png', 100, 2, 75,7);
} //end of IF
$this->useTemplate($this->_tplIdx, 0, 0,200);
} //end of HEADER
function Footer() {
if($this->page > 0) {
$this->SetY(-20);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0,0,0);
$this->Write(0, "Page", '', 0, 'C');
} //end of if
} // end of footer
} //end of CLASS
// new PDF file
$pdf = new PDF();
$pdf->addPage();
if($pdf->numPages>0) {
for($i=1;$i<=$pdf->numPages;$i++) {
$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
//$pdf->SetPrintHeader(false);
//$pdf->SetPrintFooter(false);
}
}
$file_time = time();
$pdf->Output("$file_time.pdf", "F");//, "I");
echo "Link: '<a href=$file_time.pdf>Stamped article</a>'";
?>
I've tried a lot of things like setPrintHeader(), etc. but nothing what I've found works for me. Could i please somebody to help?
Thank you.
duff

You can use the Line method to draw a line in FPDF. If you want a straight horizontal line, just make sure the ordinates (y values) for the start and end of the line are the same. Something like this for example:
$pdf->Ln(15,$pdf->y,200,$pdf->y);
You would modify the values to suit your needs and insert it in the overridden methods for Header and Footer as appropriate for your application.

Better would be to leave the two methods (Header and Footer) empty. this way you would overwrite the drawing from super-class.
like this:
class EmptyFPDI extends FPDI
{
public function Header()
{
}
public function Footer()
{
}
}

Related

FPDF : set position from inner page

Using fpdf to create my document, it works perfectly but for one thing. There is a superposition a one line on the footer part.
Here is my Pdf class header function :
function Header() {
$this->setY(40);
$titre="MY TITLE";
$this->SetFont('Arial','B',16);
.....
doing all my stuff
.....
$this->Ln();
}
Here is the Footer method :
function Footer() {
$this->SetY(-25); --postionning footer at 2.5 cm from the bottom
....
doing my stuff
}
calling program:
$pdf = new Pdf();
$pdf->Open();
$pdf->addPage();
$request= "SELECT.....";
$result = $link->query($request) ;
while($row=$result->fetchRow()) {
$pdf->SetX(18);
foreach($row as $champ=>$valeur) {
....
displaying the fields in the document
...
}
$pdf->Ln();
}
My problem : it displays 1 line too much that superposes the Footer.
I would like to give a position between the footer and the inner page. Is that possible?
Thank you
Use the SetAutoPageBreak() method to increase the bottom margin, for example:
$pdf = new Pdf();
$pdf->SetAutoPageBreak(true, 40);

FPDF FPDI How to import a page from different FPDF class?

I have a class that creates a FPDF document. And I would like to include that document in a different FPDF class.
// Document/Class 1
$pdf->new MyFirstDocument();
$pdf->output
// Document/Class 2
class MySecondDocument extends FPDF {
$this->addPage() //etc
//and from here i would like to call the
//class MyFirstDocument and import the output
//into MySecondDocument as an additional page
}
You wrote:
I have a class that creates a FPDF document. And I would like to include that document in a different FPDF class.
This is wrong! You can not have after $pdf->Output() anything output more, because $pdf->Output() creates a PDF document. You have to use it only one time per PDF document. Please read the documentation.
You can not have a second instance from FPDF too. Because of this you have to have in first class a constructer with instance from FPDF as parameter.
Solution example:
Because of all this we can not really import page from different FPDF class with FPDF, but we could do some like follows.
Code from firstpdf_class.php:
<?php
class FirstPDF_Class
{
private $fpdf_instance;
function __construct($fpdf_instance)
{
$this->fpdf_instance = $fpdf_instance;
}
function print_title($doc_title, $company)
{
$this->fpdf_instance->SetFont('Helvetica','B', 18);
$this->fpdf_instance->Cell(210,4, $company, 0, 0, 'C');
$this->fpdf_instance->Ln();
$this->fpdf_instance->Ln();
$this->fpdf_instance->Cell(37);
$this->fpdf_instance->SetFillColor(209, 204, 244);
$this->fpdf_instance->SetFont('Helvetica', 'B', 11);
$this->fpdf_instance->Cell(150,8, $doc_title, 0, 0, 'C', 1);
}
}
?>
Code from secondpdf_class.php:
<?php
require('fpdf.php');
require('firstpdf_class.php');
class SecondPDF_Class extends FPDF
{
private $printpdf;
function __construct($orientation = 'P', $unit = 'mm', $size = 'A4')
{
parent::__construct($orientation, $unit, $size);
$this->printpdf = new FirstPDF_Class($this);
$this->import_page('Document 1', 'Company "Fruits Sell"');
$this->import_page('Document 2', 'Company "Boot Sell"');
}
public function import_page($doc_title, $company)
{
$this->AddPage();
$this->printpdf->print_title($doc_title, $company);
}
function Footer()
{
$this->SetXY(100, -15);
$this->SetFont('Helvetica','I', 10);
$this->SetTextColor(128, 128,128);
// Page number
$this->Cell(0, 10,'This is the footer. Page '.$this->PageNo(),0,0,'C');
}
}
$pdf = new SecondPDF_Class();
//not really import page, but some like this
$pdf->import_page('Document 3', 'Company "Auto Sell"');
$pdf->AddPage();
$pdf->SetFont('Helvetica','B', 18);
$pdf->Cell(210,4, 'Page 3.', 0, 0, 'C');
$pdf->Output();
?>

Unwanted lines in TCPDF class with custom header and footer

I'm using TCPDF to generate pdf reports. I need custom headers and footers, so I extended the original class to overwrite the Header and Footer methods as suggested in the official documentation (https://tcpdf.org/examples/example_002.phps).
Here you are the code:
class AppPdf extends \TCPDF {
CONST LOGO_PATH = '/../../../public_html/public/images/logo-big.png';
private $xLogo;
private $yLogo;
private $wLogo;
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false, $xLogo = 8, $yLogo = 0, $wLogo = 50) {
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
$this->xLogo = $xLogo;
$this->yLogo = $yLogo;
$this->wLogo = $wLogo;
}
public function Header() {
$this->Image(__DIR__ . self::LOGO_PATH, $this->xLogo, $this->yLogo, $this->wLogo);
}
public function Footer() {
$this->SetXY(34,260);
$this->SetFont('Helvetica', 'I', 8);
$this->SetTextColor(0, 0, 0);
$this->MultiCell(130, 20, "footer text", 0, "C", false);
}
}
Then I have a base template that it is used for all the generated documents:
class BasePdf {
CONST CREATOR = 'Creator';
CONST TITLE = 'Title';
CONST PDF_FONT_NAME_MAIN = 'Times';
CONST PDF_FONT_SIZE_MAIN = 11;
protected $pdf;
public function __construct($xLogo = 8, $yLogo = 0, $wLogo = 50)
{
$this->pdf = new AppPdf('P', 'mm', 'A4', true, 'UTF-8', false, false, $xLogo, $yLogo, $wLogo);
$this->pdf->SetCreator(self::CREATOR);
$this->pdf->SetAuthor(self::CREATOR);
$this->pdf->SetTitle(self::TITLE);
$this->pdf->SetFont(self::PDF_FONT_NAME_MAIN, "", self::PDF_FONT_SIZE_MAIN);
}
public function getPdf()
{
return $this->pdf;
}
}
The base template is used as shown in the following class:
use AppBundle\Entity\HPVExam;
class HPVReport extends BasePdf
{
public function __construct(HPVExam $HPVExam)
{
parent::__construct(8, 10, 75);
$this->pdf->AddPage();
}
}
The problem is that this code generates pdfs with an annoying horizontal line in the top and another one in the footer, as you can see in the following image .
I have already tried the suggestions provided here PHP / TCPDF: Template Bug? and here Changing or eliminating Header & Footer in TCPDF but without luck.
What I'm doing wrong? It seems that the original Header and Footer methods are not correctly overwritten... Any idea? Thank you!
Just say to TCPDF that dont print the header or go and modify the source....
$pdf->SetPrintHeader(false);

custom PHP tcpdf footer with top and bottom border

First time use TCPDF, great library.
I try to create a custom footer, however i want to create a custom footer that include the page number and date inside a div with top and bottom border! So any help?
Many Thanks
Karel is right on this.
you could however ignore the Footer() function if it's getting you in trouble with the dynamic of it. seems to me that you would like to have a div in your footer.
to do this you have to get rid of the default footer first:
$this->setPrintFooter(false);
and then create your own footer function.
public function _footer($input) {
$html = $input;
$this->setY(-15); // so the footer is an actual footer.
$this->writeHTMLCell(
$width = 0, // width of the cell, not the input
$height = 0, // height of the cell..
$x,
$y,
$html = '', // your input.
$border = 0,
$ln = 0,
$fill = false,
$reseth = true,
$align = '',
$autopadding = true
);
}
the values of the above function are the defaults. so you may want to edit them.
with a call like this:
$div = '<div id="footer">wow this is a nice footer</div>'>
$pdf->_footer($div);
you create your HTML cell with the $div input.
to get the page numbers and stuff like that just checkout the TCPDF documentation page: http://www.tcpdf.org/doc/code/classTCPDF.html
hope this helps a little bit to understand it.
this is just an example from scratch.
edit it as you like and try out some stuff to get your PDF document going.
You can extend the TCPDF class and add your custom Footer function. Here's an example that I've used, see if it fits and modify to your own needs. It doesn't use a <div> to render the footer, that wasn't possible at the time I wrote this (might be now though, TCPDF is evolving rapidly).
class MyPDF extends TCPDF {
public function Footer() {
$this->SetY(-15);
$this->SetFont('helvetica', 'I', 8);
$this->Cell(0, 10,
'Page ' . $this->getAliasNumPage() . ' of total ' .
$this->getAliasNbPages(), 0, false, 'C', 0, '',
0, false, 'T', 'M');
}
public function Header() {
// add custom header stuff here
}
}
$pdf = new MyPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,
true, 'UTF-8', false);

TCPDF Multiple Footers

I am using TCPDF to generate PDF documents. The problem I have is that I am trying to add a different footer per page. Apparently, TCPDF provides a solution only for a single footer.
Each footer contains only basic html code without any styling.
Any ideas?
You can turn of the default footer like this:
$pdf->SetPrintFooter(false);
create your own one like this:
$footer = 'yau man footer stuff';
and then create your own footer function:
public function _footer($input) {
$text = $input;
$pdf->setY(-15); // or whatever location your footer should be displayed.
$pdf->Cell ( // or another method like Write(), WriteHTML() ..
$width = 0, // width of the cell, not the input
$height = 0, // height of the cell..
$x,
$y,
$text = '', // your input.
$border = 0,
$ln = 0,
$fill = false,
$reseth = true,
$align = '',
$autopadding = true
);
}
by calling $pdf->_footer($footer); you create your own footer
You can create custom Header and Footer by exporting TCPDF in class and you use that class anywhere!
class MYPDF extends TCPDF {
//Page header
public function Header() {
//Header code
}
// Page footer
public function Footer() {
//Footer code
//you have $this->PageNo() where you can add conditional UI
}
}
And you can use this class as
$o_pdf = new MyPDF([all params TCPDF requires]);
I hope this will help you! Happy coading!!!

Categories