I am currently working on a little license plate generator and I'm trying to place some text, followed by an image and followed by some text again. However, I'm hardcoding all the positioning and it's not working when the data changes (e.g smaller text results in larger space)
My question is, how do I turn my solution into a more general solution. The main issue is that if I have a text with less characters, the text is not being auto aligned and spaced again. I'm not sure why my code is not working. Here's what I wrote:
$left_text_x = 85;
$left_text_y = 50;
$middle_img_top_x = $left_text_x + 15.8;
$middle_img_top_y = $y + 0.6;
$middle_img_bottom_x = $left_text_x + 15.3;
$middle_img_bottom_y = $y + 5.1;
$middle_text_x = $middle_img_bottom_x + 4.7;
$middle_text_y = $left_text_y;
$right_text_x = $middle_text_x + 11.3;
$right_text_y = $left_text_y;
//Text
$pdf->Text($left_text_x, $left_text_y, 'AAA', false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false ); //Left
$pdf->Image('../../images/bundesland/Baden-Wurttemberg.png', $middle_img_top_x, $middle_img_top_y, 4, 4, '', '', '', false, 300); //Top img mid
$pdf->Image('../../images/plakette/rose.png', $middle_img_bottom_x, $middle_img_bottom_y, 5, 5, '', '', '', false, 300); //Bottom img mid
$pdf->Text($middle_text_x, $middle_text_y, 'AA', false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false ); //Middle
$pdf->Text($right_text_x, $right_text_y, 'AAA', false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false ); //Right
Here are my results:
Here's one that gives the unnecessary space:
Here's another one:
So as you can see, the space is not being adjusted automatically although my code looks fine. I know there is a logical error but I can't figure it out.
EDIT: MORE DETAIL ON THE FOLLOWING LINK
TCPDF Text and Image Positioning
Related
// Add a new page for TOC
$pdf->addTOCPage();
$pdf->Ln(14);
$pdf->setFont($fontname, '', 14);
//Set background page 1
$pdf->SetAutoPageBreak(false, 0);
$img_file1 = FILE_PATH.'/number/3-muc-lucko-text--trang-1.jpg';
$pdf->Image($img_file1, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
//Set background page 2
$img_file2 = FILE_PATH.'/number/3-muc-lucko-text--trang-2.jpg';
$pdf->Image($img_file2, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
$pdf->setPageMark();
$pdf->addTOC(3, $fontname, '.', 'INDEX', '', array(209,209,204));
$pdf->endTOCPage;
I'm having a problem when the table of contents is 2 pages or more but only 1 background image can be inserted.
I want to insert 2 images in the table of contents of tcpdf.
Please help or give me a solution to this problem
Right now I'm working on a Symfony web app, and it is using TCPDF to print receipts. I would like to know if there is a way/method to know how long is part of my content and compare it to the rest of the empty page. If the content is larger than the rest of the page, it would give a add another page and put the content there.
For example, I have 3 tables in my first page. If the first two tables occupy 80% of the page, and the third will ocupy 50%, compare those 50% to the 20% remaining of the first page, and since the content is bigger than the rest of the page, add another page and put table 3 there.
I don't have any code to show right now, but I am asking now since this is something I will have to do in the future (and I know there have been alot of problems with overlapping tables)
There's a function I use called public function checkHeights() that, as the name suggests, checks the heights of an element in TCPDF.
public function checkHeights($pdf, $array, $checkHeight = 10){
$max_height = 0;
foreach($array AS $item){
$current_height = $pdf->getStringHeight($item["width"], $item["text"], false, true, '', 1);
if($current_height >= $max_height){
$max_height = $current_height;
}
}
$page_height = $pdf->getPageHeight();
$margins = $pdf->getMargins();
if($pdf->GetY() + $max_height >= ($page_height - $margins["bottom"] - $checkHeight)){
$size = $pdf->getFontSizePt();
$style = $pdf->getFontStyle();
$pdf->SetFont('helvetica', 'I', 6.5, '', true);
$pdf->SetColor("text", 155, 155, 155);
$pdf->Cell(0, 0, 'Continued...', 'T', 0, 'R', 0, '', 0, false, 'T', 'T');
$pdf->SetFont('helvetica', $style, $size, '', true);
$pdf->SetColor("text", 0, 0, 0);
$pdf->addPage();
$pdf->Cell(0, 0, '', 'B', 1, 'R', 0, '', 0, false, 'T', 'T');
}
return $max_height;
}
It accepts 3 variables: Your $pdf object, an array of arrays containing cell information and the height (space from the bottom) to check against.
There are two ways to use this function. Firstly, checking to see if a single Cell will take up the remaining space on a given page:
self::checkHeights($pdf, array(array("width" => 0, "text" => "")));
If there isn't enough space, it will save your font and style settings and add a small Continued... to the right side of your page.
Secondly, passing in the contents of a row (or list of Multicell items):
$height = Self::checkHeights($pdf, array(array("width" => 45, "text" => "Example"), array("width" => 54, "text" => "Example"), array("width" => 52, "text" => "Example"), array("width" => 26, "text" => "Example"), array("width" => 0, "text" => "Example")));
In this example, the current row item has 5 cells, with widths of 45, 54, 52, 26 and 0 (0 being the remaining horizontal space). It also takes into account cell wrapping (ie making the row grow in height to accommodate text overflow) and will add the Continued... text if it's too long.
Once you have $height defined and a new page added (if necessary), you would create your row of Multicells:
$pdf->MultiCell(45, $height, "Example", 'L', 'L', 0, 0, '', '', true, 0, false, true, $height, 'M');
$pdf->MultiCell(54, $height, "Example", 0, 'R', 0, 0, '', '', true, 0, false, true, $height, 'M');
$pdf->MultiCell(52, $height, "Example", 0, 'R', 0, 0, '', '', true, 0, false, true, $height, 'M');
$pdf->MultiCell(26, $height, "Example", 0, 'C', 0, 0, '', '', true, 0, false, true, $height, 'M');
$pdf->MultiCell(0, $height, "Example", 'R', 'R', 0, 1, '', '', true, 0, false, true, $height, 'M');
Refer to the TCPDF documentation for using ->MultiCell() function, but the important ones are the ones that use $height and the last one, which is vertical alignment.
Essentially, checkHeights() is a way to construct cells in a row that all have the same height, based on the content, but also a way to check if a cell will use up the remaining vertical space on the page, and add a page before outputting a new cell. If you need more clarification, let me know.
I'm using TCPDF and got the following function that adds page number to the footer. However this only centers the page numbering, i want to be able to add a description on the left and a reference number on the right. So in other words 3 columns, left column being left aligned with description, middle column with page number and centered and right column with reference number and right aligned.
class MYPDF extends TCPDF {
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('Calibri', '', 8);
// Page number
$pageNumbers = 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages();
$this->Cell(0, 10, $pageNumbers, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Easiest thing to do is create three separate cells, one for each item, as follows:
$this->Cell(30, 10, 'Description', 1, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Cell(130, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 1, false, 'C', 0, '', 0, false, 'T', 'C');
$this->Cell(30, 10, 'Reference number', 1, false, 'C', 0, '', 0, false, 'T', 'M');
This will create footer looking like this (I turned $border to '1' so the border shows how the cell structure works). Of course you can adjust the sizes of the different cells if you need more space for the content.
I want to create a footer for a PDF document that contains the date left aligned, the creator centered and the page right aligned. These should be in a single line. I tried the following code:
$this->Cell(0, 10, $date->format('d.m.Y'), 0, false, 'L', 0, '', 0, false, 'T', 'M');
$this->Cell(0, 10, 'Creator', 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
The creator is shifted to the right and overlays with the pages:
Does anybody have a solution for that problem?
You need to set the width of the Cell(), as according to the docs http://www.tcpdf.org/doc/code/classTCPDF.html#a33b265e5eb3e4d1d4fedfe29f8166f31 your $date->format('d.m.Y') Cell() is extending to the right margin, forcing the other cells on the line to the right margin.
$w (float) Cell width. If 0, the cell extends up to the right margin.
Try something like (may have to adjust based on font size)
$this->Cell(20, 10, $date->format('d.m.Y'), 0, false, 'L', 0, '', 0, false, 'T', 'M');
$this->Cell(20, 10, 'Creator', 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Cell(20, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
I ran into alignment issue with TCPDF too. I noticed that if you use x-coordinate as 0, then it will use the attribute 'R' for right alignment. But if it's set to a non zero value then it ignores the 'R' setting. Here's the statement I used for right alignment.
$this->Cell(0, 9, 'Text-to-be-aligned-right', 0, false, 'R', 0, '', 0, false, 'T', 'M' );
I have created a custom header for my PDF created with TCPDF. Now I would like to add a blue line (about 2px width) that goes across the page at the bottom of the header but can't figure out how?
I believe you do it like this:
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$pdf->Line(5, 10, 80, 30, $style);
Here is the full example
http://www.tcpdf.org/examples/example_012.phps
I found the easiest way to put line
$pdf->writeHTML("<hr>", true, false, false, false, '');
You can also use the page axis:
$pdf->Line(5, $pdf->y, $pdf->w - 5, $pdf->y);
However if you are trying to render a colored <hr> html tag you will need to adjust the TCPDF::DrawColor (this excerpt is from code that adds a graph bar to each row of a data report as per $twidth and $lengthmm):
$htmlbar = '<hr style="width:' . $lengthmm . 'mm;">';
$oldDrawColor = $pdf->DrawColor;
$pdf->setDrawColor(121, 161, 46);
$pdf->MultiCell($twidth,'2',$htmlbar,0,'L',$fill,1,'','',true,0,true,false,4,'T',false);
$pdf->DrawColor = $oldDrawColor;
Drawing a horizontal black line at the current position:
$style = ['width' => 0.2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [0, 0, 0]];
$this->pdf->SetLineStyle($style);
$this->pdf->Line(PDF_MARGIN_LEFT, $this->pdf->getY(), $this->pdf->getPageWidth()-PDF_MARGIN_LEFT, $this->pdf->getY());
$this->pdf->Ln();
The point is to get the x value for the second point.
This is how I do it:
$pageWidth = $pdf->getPageWidth(); // Get total page width, without margins
$pageMargins = $pdf->getMargins(); // Get all margins as array
$headerMargin = $pageMargins['header']; // Get the header margin
$px2 = $pageWidth - $headerMargin; // Compute x value for second point of line
$p1x = $this->getX();
$p1y = $this->getY();
$p2x = $px2;
$p2y = $p1y; // Use same y for a straight line
$style = array();
$this->Line($p1x, $p1y, $p2x, $p2y, $style);
Links
TCPDF::getMargins()
http://www.tcpdf.org/doc/code/classTCPDF.html#ae9bd660bf5b5e00eea82f1168cc67b5b
TCPDF::getPageWidth()
http://www.tcpdf.org/doc/code/classTCPDF.html#a510ab21d6a373934bcd3bd4683704b7e
Have fun!
Just add some HTML : )
$html ='<hr>';
$pdf->writeHTML($html, true, false, true, false, '');