Auto fitting a cell to the size of the content? - php

I'm new to tcpdf, creating my first document.
I wonder if there is a way to fit the width of the cell automatically to the content. I currently only see options for fixed size, or taking the whole page width until the end of the line.
I'm aware of GetStringWidth() but having the following problems with it
Why should one bother even for this? Is there a way to just make the cell fit automatically to its contents width?
GetStringWidth() seems to err from time to time, giving shorter results the actual, thus causing the text to be split to the next line. A font is set.

After learning TCPDF more, this is the conclusion:
Cell() and MultiCell() are not intended to be used for just outputting a string and fitting it's length. Instead, Write() and WriteHtml() should be used. Cells exist for the case where you actually want to control the dimentions of the field manually.
Nevertheless, in some cases one may want to compute the width of the cell, such that it takes into account the sizes of the text inside. For this purpose exists GetStringWidth(). (Unfortunately for me it err's from time to time. Maybe I'm not aware of something)

Have an internal "Legacy" application that uses TCPDF to generate a PDF of a checklist. We recently moved from creating a giant string of HTML that described a table, created using the $pdf->writeHTML($myHTMLString); method, to using the MultiCell() methods.
However, we ran into an issue where some text in a description cell would need to run on to a second line, this threw off our layout. As a fix, we created an if block based on 2 variables, one for the string width the other for the actual cell width. (We had 2 instances where the cell width might vary).
If block example:
// Get width of string
$lWidth = $pdf->GetStringWidth(strip_tags($clItem['description']));
// Determine width of cell
$oadWidth = (.01*$width[0])*186;
if ($lWidth < $oadWidth) {
$cHeight = 3.5;
} else {
$cHeight = 7;
}
We then used the variable created by the if block in the MultiCell() like this
$pdf->MultiCell((.01*$width[0])*186, $cHeight, strip_tags($clItem['description']), 1, 'L', 1, 0, '', '', true);
We reused the $cHeight variable for the height params in the other sibling cells so each row of cells had a uniform height. You could most likely reuse this method with any of the other right functions that have a height parameter in TCPDF. Thanks to #shealtiel for the original reference to GetStringWidth()

Related

PHPExcel duplicate style only duplicate the first cell even when range of cell is given

I'm trying to copy styles from a range of cells in a template file and paste it to another range of cells in another file and then save the resulting file. I experimented on a small subset of my Excel file before I fully implemented it, but I found a strange result.
Here is my code snippet...
include "../class/PHPExcel.php";
include "../class/PHPExcel/IOFactory.php";
include "../class/PHPExcel/Cell.php";
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$frTemplate = $reader->load("document/Template/FR-001 Template.xlsx");
$frTemplate->setActiveSheetIndex(0);
$frTemplate->getActiveSheet()->setCellValueExplicitByColumnAndRow(3, 6, $year);
//copy style
$cellStyle = $frTemplate->getActiveSheet()->getStyle('E47:E49');
$frTemplate->getActiveSheet()->duplicateStyle($cellStyle, 'E52:E54');
//copy value
$cellValues = $frTemplate->getActiveSheet()->rangeToArray('E47:E49');
$frTemplate->getActiveSheet()->fromArray($cellValues, null,'E52');
$writer = PHPExcel_IOFactory::createWriter($frTemplate, "Excel2007");
$writer->save("document/Template/FR-001 2017.xlsx");
echo "Done";
Here is an image of the copied Excel file...
Copied File-Case 1
And here is an image of the target file...
Result-Case 1
I'm trying to apply the code with another data, and I found that -
"Merge cell" style cannot be copied
When a range of cells is copied, the only style copied is the first cell (upper left)
Sadly, I cannot post the evidence of the above case as my reputation is still very low (It is my first question in Stack Overflow!)
So, can you please tell me what is wrong with my code? Thanks in advance.
That's correct:
"Merge cell" is not an aspect of style; it's part of the structure of the worksheet, so it cannot be "copied" by duplicateStyle. If you want to merge cells, you need to use the mergeCells() method for each merge range that you want to set.
The duplicateStyle() method copies a style to a range of cells; it doesn't copy multiple styles to a new range. The getStyle() method allows a range for the purpose of setting a style against all cells in that range, but only returns the style of the first cell within that range. You need to duplicate each different style to its own range.

FPDF right margin values have no effect

I am using the PHP extension FPDF to create PDF documents. I believe I have the margins set to 1" all the way around with justified text, but when I create a PDF document, download it and open it in Acrobat, I see the left margin is 1" but the right margin is about 1.2". I've gone through fpdf.php looking for the issue and cannot find it.
$oPdf = new myPDF('P', 'mm', 'letter');
$oPdf->Open();
$oPdf->SetMargins(25.4,25.4,25.4,25.4);
//set default font/colors
$oPdf->SetFont('Times', '', 12);
$oPdf->SetTextColor(0,0,0);
$oPdf->SetFillColor(255, 255, 255);
//add the page
$oPdf->AddPage();
$oPdf->AliasNbPages();
If I change the top and left margin values, the document reflects the changes. If I change the bottom or right, there is no change. I've seen how the bottom can be changed wth set auto default page break, answered elsewhere. My question is how can I change the right margin and it actually take effect in my document? I have a feeling that this has to do with the measurement of the text when FPDF calculates whether the justified text will fit on the line. I've checked the width of the line and it outputs 165.1 (which is 6.5 inches x 25.4mm) Completely stumped.
Ok, I feel kind of dumb, but hopefully this will help others. I use the FpdfMulticell (See http://www.interpid.eu/fpdf-multicell) for more control over cells and other parts of displaying the content on the PDF document. This is great and all, except for the redundancies that are created. The components of this function require width and height for the cell to be created. This is where the problem was. For some reason I was passing 160 to it rather than 165.1. 6.5" x 25.4 = 165.1. When I go through the references to create the multiCell function within the class, it fixes my problem.
If you're going to use the multicell, I suggest declaring a variable at the beginning of your page such as $pw (i.e., page width) and then instead of passing a constant value, pass the variable. That way your values are uniform and if you want to change it up, then you only have 1 value to change. Make sure you comment so you know what the purpose of that variable is.
$oMulticell = new FpdfMulticell($oPdf);
$pw = 165.1
//Make sure that the content is long enough to go to another line or you won't
//notice the difference with the justified text
$content = "Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here ";
$oMulticell->multiCell($pw,10,$content,0,"J",1,0,0,0,0);
This works!

tcpdf auto size text field

I am having trouble getting a text field to auto size on a form I'm creating. I am setting the textSize of the properties to 0 in the properties of the TextField but it seems to just use the font set for the document. Here is the code I am using to create the TextField.
$pdf->TextField('description', 195, 32, array('multiline'=>true, 'lineWidth'=>0, 'borderStyle'=>'none', 'textSize'=>0), array('v'=>$description));
I know a kinda round-about way of possibly fixing this..
Before your textfield code, what ever you would like your font to be (not the default for the entire document) you could always try:
$pdf->SetFont('fontName','',fontSize);
And after your textfield code, just set the font back to what ever you wanted it to be for the rest of the document.
Not really sure if this was exactly what you were looking for, but I hope it helps. Good luck with it all.
I faced also that exact problem. I had a long time before I could find out the solution to this.
To make the field auto size the textual content, the font size of the field must be set to 0. It looks like TCPDF doesn’t support the javascript textSize option (see at the end of TCPDF_STATIC::getAnnotOptFromJSProp() ), so we must set the font size to 0 before the field, and set back again to its original value after the text field :
// Beware to use getFontSize*Pt* and not getFontSize.
// getFontSizePt gets the size in pt, and getFontSize gets the size in user units
// (units you gave when you created TCPDF object or default to mm)
// We need to set it again back with SetFontSize() which takes the font size in pt
$originalFontSize = $pdf->getFontSizePt();
$pdf->SetFontSize(0);
$pdf->TextField($name, $w, $h);
$pdf->SetFontSize($originalFontSize);

Get the width and height of a string in pixel (PHP)

Is there any good function to calculate the pixel width and height? Can imagettfbbox be used to do that? I mean which ttf file is needed for the different fonts used by different browsers?
Thx
As PHP run's on the server there is no way to do that with PHP alone. The size of the text depends on the operating system, the browser (and the browser settings like zoom) of the client. You could however use javascript to get the size of an element once its rendered.
height = document.getElementById("elementId").style.height; // Will return 176px for example
width = document.getElementById("elementId").style.width; // Will return 176px for example
Hope this helps
There is a function (imagefontwidth and imagefontheight), but they only return the size of one character using the built-in fonts (which are all fixed-width). You'll get the width/height of the entire string by multiplying the number returned by the number of characters.
I don't know of any way that lets you get the width or height of a string using a custom font, but I'm interested in this too.
Hope this helps.

PDFlib give pdf_fit_textline a border

I'm generating PDFs dynamically using PHP and PDFlib and some of the fields require text being centered on blank fields lines (like you would see on a paper form). To make sure I have the text field using the maximum width available to me as well as positioned properly I would like to place a temporary border around it so I can see where it is and its size. Unfortunately I do not see any way to do this in the PDFlib manual.
Does anyone know of a tip, trick, or obscure documentation that will allow me to either give these fields a border or some other means of determining their exact size and position?
You can put a showborder=true into the various fit/fill calls' optional parameters argument, and PDFlib will draw a simple solid border around the box:
PDF_fit_textline($handle, 'blah blah blah', $fontsize, $x, $y, 'boxsize={ 50 100 } showborder=true');

Categories