TCPDF Multiple Footers - php

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!!!

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);

How to remove Underline from anchor link of a pdf in fpdf

we are making a pdf report by using FPDF in which we have links.
our problem is that we are not able to remove underline and default color from the link we have sent in the pdf report. and we want to set a custom different color on different links.
below is what we are making.
$text22 = preg_replace('/\S*\b('. $searchphrase[$rr] .')\b\S*/i', '$1', $aaa);
$text22 = preg_replace('/\S*\b('. $searchphrase[$rr] .')\b\S*/i', '$1', $aaa);
$pdf->WriteHTML(utf8_decode($main));
below is our pdf report, now we have to remove underline from from the link and set custom color on that.
You may have to extend the FPDF class yourself and change the underline / colour in the PutLink function.
http://www.fpdf.org/en/script/script50.php
Edit
Here's a code sample of extending the FPDF class. In fact, as the WriteHTML function isn't in the FPDF class, this extends the class which has it in instead. This is one of many ways to make it work. In this example you have to specify the link colour in an additional attribute (data-color), as the class can't read CSS rules. You could of course write a regex to parse the CSS, and then translate the colours into r,g,b. But for a simpler example, I left that out.
<?php
// This class extends the Tutorial 6 class, which in turn extends the main FPDF class
class XPDF extends PDF
{
protected $clr = "";
function OpenTag($tag, $attr)
{
parent::OpenTag($tag, $attr);
if ($tag == "A")
{
if (isset($attr['DATA-COLOR']))
{
$this->clr = $attr['DATA-COLOR'];
}
else
{
$this->clr = "";
}
}
}
function CloseTag($tag)
{
parent::CloseTag($tag);
if ($tag == "A")
$this->clr = "";
}
function PutLink($URL, $txt)
{
// Put a hyperlink
if ($this->clr != "")
{
$clrs = explode(",", $this->clr);
$this->SetTextColor($clrs[0], $clrs[1], $clrs[2]);
}
else
{
$this->SetTextColor(0,0,255);
}
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
}
$html = 'This is some text and here is a link. To specify the pdf colour of a link, you need to specify it as RGB numbers in a data-attribute, like this or this.';
$pdf = new XPDF();
// First page
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->WriteHTML($html);
$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);

header and footer line in FPDI/TCPDF

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()
{
}
}

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);

Categories