How to add custom footer with styles in TCPDF - php

I am using TCPDF , I am trying to add custom footer with different styles like right side Customer Name: color should be blue and left side Approved By: color should be green, and bottom of the page displaying page numbers.
I have tried this in pdf page
$html_content = "<table><tr><td>Customer Name:</td><td style='color:blue;'>Suneel</td><td>Approved By:</td><td style='color:green;'>Srinu</td></tr></table></hr>"
$tcpdf->xfootertext($html_content);
It's Working, but it's not accept the styles
In TCPDF class
function Footer()
{
$year = date('Y');
$footertext = sprintf($this->xfootertext, $year);
$this->writeHTMLCell(0, 0, '', '', $footertext, 0, 0, false,true, "L", true);
$this->SetY(8);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 27, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}

Finally I found answer, Footer has accepted the all styles which we will parse the dynamic content with different colors.
Here is the solutions
$this->writeHTML($footertext, false, true, false, true);
Instead of
$this->writeHTMLCell(0, 0, '', '', $footertext, 0, 0, false,true, "L", true);
I have changed writeHTML instead of writeHTMLCell it accept the footer colors as well.

Related

margins and right align footer tcpdf

I am working on generating content for an invoice but i need to have all my 'divs' in alignment - header, body, and footer. Currently i have the header and body in alignment but the footer's margins dont seem to align plus i have two cells - one left aligned and the other right aligned. However they don't align plus their margins are off.
public function Footer()
{
$this->SetY(-15);
$this->SetFont(PDF_FONT_NAME_MAIN, ' ', 8);
$this->SetFooterMargin(15);
//Get time
$date = date('m/d/Y');
$time = date('H:i:s A');
$this->Cell(0, 0, 'Date: '.date("Y-m-d")." - Time: ".$time , 0, false, 'L');
// Page number
$this->Cell(0, 0, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, true, 'T', 'M');
}
Any suggestions?
Within the footer function, call this
$this->SetRightMargin(N); // Where N is the value of the margin spacing you like to achieve
Hope this helps

Fpdf Send To back Change order of Cell

How to send cell() to the back because it currently show to pdf based on order in the code. This is the example.
$pdf->SetXY(10, 20);
$pdf->SetFillColor(240);
$pdf->Cell(10, 10, '', 0, 1, 'L', true); // Show up 1st: Cell 1
$pdf->SetXY(10, 20);
$pdf-> SetFillColor(100);
$pdf->Cell(10, 10, '', 0, 1, 'L', true); // Show up 2nd: Cell 2
So, how can I set which Cell will come first/send to the back without follow the order?

Add padding to a cell in TCPDF

I have to make invoices with TCPDF. I have a logo on top of the pdf and the title of the company has to come right next to it.
Now i'm doing this:
$pdf->Image(__DIR__.'/../../../assets/img/logo.png', 15, 10, 10, 0);
$pdf->Cell(110, 0, 'Company', 0, 0, 'L', 0, '', 3);
But "Company" is on top of the logo now. Is there a way to add padding left on that cell?
Try using:
$pdf->Image(__DIR__.'/../../../assets/img/logo.png', 15, 10, 10, 0, null, null,'T');
documentation

How to scale bottom-of-page text using TCPDF and keep it on the page?

I'm using TCPDF to create barcode labels. The code works great until I tried to scale the price text to make it taller (more visible). Now the bottom row of labels are spilling components onto later pages, including components that are not scaled. It appears the ordinate value of the text along the bottom row of labels is off the bottom of the page and that is causing the problem, but I thought that setting the (x,y) value when calling procedures like MultiCell absolutely set the (x,y) value?. Does anyone know how to fix this? Can I force a large art board? Can I move the object after it has been scaled? Can I somehow recover from the off-page ordinate calculation?
The issue is in the create_label function, below. The second MultiCell line is the scaled entry. The specific effect on the bottom row of labels is that the description and price are correctly on the first label, but the barcode is forced to a second page. The description and price of the second label are on the second page, but the barcode is forced to a third. Etc.
function create_label($posx, $posy, $upc, $price, $desc){
global $START_X, $START_Y, $INC_X, $INC_Y, $style, $pdf;
$pdf->SetFont('helvetica', '', 15);
$pdf->MultiCell(1.00, 0.575, $desc, 0, 'L', false, 1, $START_X+($posx*$INC_X), $START_Y+($posy*$INC_Y), true, 1, false, true, 0.575, 'M', true);
$pdf->SetFont('', 'B', '24');
$pdf->StartTransform();
$pdf->ScaleY('200',1.00,0.7+($posy*$INC_Y)); break;
$pdf->MultiCell(1.00, 0.575, $price, 0, 'R', false, 1, $START_X+($posx*$INC_X)+1.00, $START_Y+($posy*$INC_Y), true, 1, false, true, 0.575, 'M', true);
$pdf->StopTransform();
$pdf->write1DBarcode($upc, 'UPCA', $START_X+($posx*$INC_X), $START_Y+($posy*$INC_Y)+0.3125+0.2625, 2.0, .3, '', $style, 'N');
}
$pdf = new TCPDF('P', 'in', 'LETTER', true, 'UTF-8', false);
$pdf->SetMargins(0.1875, 0.5, 0.1875);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
$pdf->SetAutoPageBreak(TRUE, 0.5);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->AddPage();
$START_X = 0.85;
$START_Y = 0.38;
$INC_X = 2.375;
$INC_Y = 0.9375;
// label width = 2.25
// label height = 0.875
// Label gutter = 1/8 x 1/16
$upc_list = array( /*Array of UPC numbers*/ );
for($row = 0; $row <= 10; $row++){
for ($col = 0; $col <= 2; $col++){
unset($upc);
if(strlen($upc) > 0 && strlen($upc) < 13 && is_numeric($upc)) {
unset($item);
$item = get_item_from_database($upc); // Creates data array
create_label($col, $row, $upc, $item['price'], trim($item['description']));
}
}
}
$pdf->Output('test_pdf.pdf', 'I');

PHP TCPDF remove header's bottom border

I am trying to create a header in TCPDF, however it always have a border underneath of it. Is there a way I can remove the bottom border?
This works for some versions:
// Call before the addPage() method
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
If you don't want to subclass or change the tcpdf source just call the setHeaderData method and specify white line color.
$pdf->setHeaderData('',0,'','',array(0,0,0), array(255,255,255) );
Problem solved by extends the TCPDF class and modify the header and footer.
class MYPDF extends TCPDF {
public function Header()
{
$image_file = K_PATH_IMAGES.'pdf-header.jpg';
$this->Image($image_file, 160, 10, 40, '', 'JPG', '', 'T', false, 20, '', false, false, 0, false, false, false);
$this->SetFont('helvetica', 'B', 10);
}
public function Footer()
{
$this->SetY(-15);
$this->SetFont('helvetica', 'I', 8);
}
}
tcpdf.php:
// print an ending header line
$this->SetLineStyle(array('width' => 0.25 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 255, 255)));
If the other solutions posted in this thread does not work, I solved in this way:
TL;DR
In Footer() function in Tcpdf class (tcpdf.php):
Replace this lines:
$this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L'); // line 3527 in version 6.3.1
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R'); // line 3530 in version 6.3.1
With this lines
$this->Cell(0, 0, $pagenumtxt, 0, 0, 'L');
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 0, 0, 'R');
Alternative way 1
Comment this lines in Footer() function in Tcpdf class (tcpdf.php):
In my file (version 6.3.1) they were placed at line 3524
//Print page number
if ($this->getRTL()) {
$this->SetX($this->original_rMargin);
$this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
} else {
$this->SetX($this->original_lMargin);
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
}
This will disable the render of the page number in the footer but at least it removes the unwanted line.
Alternative way 2
If these methods does not work, search in the tcpdf.php file for this string:
$this->SetLineStyle(array
You should find 3 occurrences, replace the 'color' property of the array with the value [0,0,0] (or the rgb color of your background), this should cause the line to turn white (or the color you set).
I used this method to troubleshoot where the problem was by putting custom weird color and seeing which one was being rendered.
Explanation
The line is rendered because in the lines
$this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
The border property is set to 'T' (top border). You can disable the border by set the border property to 0 (see docs here, $border param).
If that doesn't work you can entirely disable the render of the page number (and thus the border), or you can set the border to a custom color that matches you actual background.
Comment this line in Header() function of tcpdf Class :
$this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');

Categories