Putting padding between string and border in TCPDF - php

How would I put padding between a string and the border to said string in TCPDF. I'm not seeing any way to do that in the Text() method.
Do I have to use another method? Is there anyway to accomplish this?
This would be an example of code I'm using:
$pdf->Text(130, $margintop, $fieldkey, false, false, true, 'B', 0, '', false, '', 0, false, 'B', 'T', false);

TCPDF assigned default-values to the cell-paddings, which can be overwritten:
$pdf->setCellPaddings( $left = '', $top = '', $right = '', $bottom = '');
Or, with using the same padding for all sides:
$pdf->setCellPadding( $padding );
See the documentation here: http://www.tcpdf.org/doc/code/classTCPDF.html#a374b24751bf76e4ca5ba7694f87ec2c1
Update 22.04.2020: https://tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_SetCellPadding
Update 20.08.2020:
https://tcpdf.org/docs/srcdoc-copy/TCPDF/class-TCPDF/#_SetCellPadding

Related

PHP TCPDF Font Size in MM

in this moment i spend many hours to find a solution to scale a Text in TCPDF in mm.
Is it possible to scale a Text to 190mm width and automatic high?
I need only one Word that is every time scaled to 190mm width.
I dont found something that solve my problem. Maybe you have a idea?
Thank you and have a nice holiday :)
I test several things that i found in google
Maybe i am searching for the wrong things, or maybe i dont have the right keywords?
$pdf->setPageUnit('mm');
$document_width = $pdf->pixelsToUnits('100');
$document_height = $pdf->pixelsToUnits('100');
$x = $pdf->pixelsToUnits('190');
$y = $pdf->pixelsToUnits('190');
$font_size = $pdf->pixelsToUnits('190');
$txt = 'AAAg';
$pdf->SetFont ('helvetica', '', $font_size , '', 'default', true );
$pdf->Text ( $x, $y, $txt, false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false );

How to add custom footer with styles in TCPDF

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.

How to auto resize the cell in fpdf using php

I need to auto adjust the cell size depends upon the text . I have the following code .
$pdf->Cell(50,10,$name,1,0,'L',0);
If the $name exceed 10 character means the $name wrap into the next cell. How to fix?
I refer to: GetStringWidth and SetFont. As pointed out there, to use this method, a font must be selected. So I assume, you already have something like:
.
.
$pdf->AddPage();
$fontFamily = 'Courier'; // 'Courier', 'Helvetica', 'Arial', 'Times', 'Symbol', 'ZapfDingbats'
$fontStyle = 'B'; // 'B', 'I', 'U', 'BI', 'BU', 'IU', 'BIU'
$fontSize = 12.0; // float, in point
$pdf->SetFont($fontFamily, $fontStyle, $fontSize);
To adjust the cell width to the length of the text, let its value take the calculated length of the text:
$width = $pdf->GetStringWidth($name);
$height = 10.0;
$border = 1;
$ln = 0;
$align = 'L';
$fill = FALSE;
$pdf->Cell($width, $height, $name, $border, $ln, $align, $fill);
Havn't tested it, just read the manual. Hope it works.
you can use this code to solve this problem... here the concept is only if string size greater than your cell width then font size will reduce.
$pdf->CellFit($table_head2[7],$table_height,$item['remark'],1,0,'C',0,'',1,0);
check this URL
how to auto adjust cell width in fpdf using php and mysql

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