Getting text height to know fill height as in TCPDF
I'm trying to go through the code of TCPDF to understand how it calculates the height of the text to be rendered, but it's too much for me to handle without asking.
What I want to know: in the PDF from example 5 http://www.tcpdf.org/examples/example_005.pdf it gives the cell a yellow background. I'm guessing that at the basic level, it first draws a box with this fill color, then adds the text, so what method is it calling to get the height of the text to know the height of the box to fill?
I can see from the example code that MultiCell()
is the entry point, but it's not clear what's the method it calls to get the height of the text. I pasted the cod开发者_StackOverflow中文版e for MultiCell()
in this pastebin
http://pastebin.com/A1niGrQG
Anyone knows how to trace this, because doing it by hand and looking through the code isn't working at all for me.
TCPDF (at least the latest version) includes the method getStringHeight()
which get the estimated height needed for printing a simple text string using the Multicell()
method.
Additionally, the getNumLines()
method gives you the estimatad number of lines.
Check the source code documentation at http://www.tcpdf.org for further information.
The cell is being drawn by MultiCell: http://www.tcpdf.org/examples/example_005.phps
$pdf->MultiCell(55, 5, '[LEFT] '.$txt, 1, 'L', 1, 0, '', '', true);
and from: http://api.joomla.org/com-tecnick-tcpdf/TCPDF.html
int MultiCell (float $w, float $h, string $txt, [mixed $border = 0], [string $align = 'J'], [int $fill = 0], [int $ln = 1], [int $x = ''], [int $y = ''], [boolean $reseth = true], [int $stretch = 0])
So as you can see, the first two values are statically assigning a width (55) and a height (5) to the MultiCell
Additionally:
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
You can see the unit of measurement is the program/class default PDF_UNIT
The font size is then set with
$pdf->SetFont('times', '', 10);
(or just use SetFontSize for the size only)
Just as a very simple proof of concept for what i said in my last comment on my other answer...
** You need to use a monospaced font and a line height equal to your text height (that or modify the code for line height instead of text height) fairly simple fix...
You also have to figure out your aproximate monospaced width.. Best thing to do is use a capitol M (M is the widest char - so monospaced chars are set to this width..)
<html><head></head><body style="font-family:'Courier New', Courier, monospace; line-height:12px;">
<?php
//If you are using a monospace font, this kinda works
$divWidth = 300; // in px;
$fontSize = 12; // (in px);
$fontWidth = 7; // in px - aprox monospace font width
$lineChars = floor($divWidth / $fontWidth);
$text = <<<EOT
MMMMMMMMMM (capital M is the widest character)I'm trying to go through the code of TCPDF to understand how it calculates the height of the text to be rendered, but it's too much for me to handle without asking.
What I want to know: in the PDF from example 5 it gives the cell a yellow background. I'm guessing that at the basic level, it first draws a box with this fill color, then adds the text, so what method is it calling to get the height of the text to know the height of the box to fill?
I can see from the example code that MultiCell() is the entry point, but it's not clear what's the method it calls to get the height of the text. I pasted the code for MultiCell() in this pastebin
EOT;
$wrappedText = wordwrap($text, $lineChars, "LINEHERE");
$lines = substr_count($wrappedText, "LINEHERE");
$newlines = substr_count($text, "\n");
$text = str_replace("\n", "<br>",$text);
$lines += $newlines;
$divHeight = $lines * $fontSize;
echo "With a width of: " . $divWidth . "<br>";
echo "Number of Lines: " . $lines . "<br>";
echo "Height Required: " . $divHeight . "px<br>";
echo "Wrapped Text at: " . $lineChars . " characters<br><br>";
$divsize = "width:$divWidth px; height:$divHeight px; font-size:$fontSize px; ";
$outStr = "<div style='overflow:auto; display:inline-block; background-color:aqua; $divsize'>$text</div>";
$outStr .= "<div style=' display:inline-block; background-color:fuchsia; $divsize'> </div>";
echo $outStr;
?>
</body></html>
精彩评论