I need to remove a watermark on the last page of my PDF, but I can't detect the last page in the header Yes I can do it in the footer.
protected $last_page_flag = false;
public function Close() {
$this->last_page_flag = true;
parent::Close();
}
public function Footer(){
if ($this->last_page_flag ){
//this work fine
}
}
//But I can't detect the last page in the header, which is where I put the watermark.
public function Header(){
$img_file = K_PATH_IMAGES.'marca_agua.jpg';
$this->Image($img_file, 12, 80, 185, 180, '', '', '', false, 300, '', false, false, 0);
$this->setPageMark();
//if I do
if ($this->last_page_flag ){
//Do nothing...
}
}
My pdf has a dynamic number of pages and I need to remove the watermark on the last one. How can I detect if it is the last page in the header?
Or how can I insert the watermark in a section other than the header?
Thanks! I am really desperate with this problem because it is the last thing I have left to deliver a job and I have been trying it for two days without a solution.
You have to update the value of $tcpdflink to false in /tcpdf/tcpdf.php
/**
* If true print TCPDF meta link.
* #protected
* #since 5.9.152 (2012-03-23)
*/
protected $tcpdflink = false;
Also (or instead), update the value on the constructor:
$this->tcpdflink = false;
Related
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);
Good Day Guys! I need some help when using TCPDF, I have this problem where the first page renders the details ok but on the second page, all the details are stacked at one area, I'm using MultiCell for the details.
public function PageHeader() {
$this->SetMargins(5, $this->GetY(), -1, true);
$this->SetCellPadding(0);
}
private function _addPage() {
$top_margin = 50;
$this->AddPage();
$this->setCellHeightRatio(1.25);
$this->SetMargins(5, $top_margin, 5, true);
$this->SetTopMargin($top_margin);
}
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);
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!!!
I decided to make Yii captcha render random colors for background and foreground, so I had made the following change to the public method actions in the SiteController where the captcha is going to be rendered in the actionContcat view.
class SiteController extends Controller
{
/**
* Declares class-based actions.
*/
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>$this->setRandColor('DARK'),
'foreColor'=>$this->setRandColor('LIGHT'),
),
// page action renders "static" pages stored under 'protected/views/site/pages'
// They can be accessed via: index.php?r=site/page&view=FileName
'page'=>array(
'class'=>'CViewAction',
),
);
}
...
In the code above I have make the backColor and foreColor keys values are the return of a private method setRandColor. The following is the method code:
/**
* Generate random hexadecimal color code in format 0xXXXXXX according to type param
* which has only two values DARK and LIGHT
* #param string $type
*/
private function setRandColor($type='DARK')
{
$color = '0x';
$darks = array(0,1,3,4,5,6,7);
$lights = array(9,'A','B','C','D','E','F');
if ($type == 'DARK')
{
$chooseFrom = $darks;
}
else
{
$chooseFrom = $lights;
}
for ($i = 0; $i < 6; $i++)
{
$color .= $chooseFrom[array_rand($chooseFrom)];
}
return $color;
}
I have tested setRandColor alone. i.e in plain php script and I found it works fine to return the hexadecimal code. Look at the following Demo: http://codepad.viper-7.com/OcCSjL
However, when using the described code above I just get a black captcha image with no any error messages. I need to know why this code do not work in my Yii application?
I have just found the problem. The problem is the type of the returned value from setRandColor method. The method returns string value, while the captcha array requires hexadecimal value.
I solved this issue by modifying the last line of setRandColor in which it returns the value as follows:
return $color/1;
By this way I casted the type from string to be number.