Getting the height of the text to determine the height of filling, as in TCPDF

I am trying to execute the TCPDF code to understand how it calculates the height of the text to be displayed, but it is 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 assume that at a basic level, he first draws a box with this fill color, then adds text, so what method does he call to get the height of the text to find out the height of the field to fill?

I can see from the sample code what MultiCell()is the entry point, but it is not clear which method it calls to get the height of the text. I pasted the code for MultiCell()in this pastebin

http://pastebin.com/A1niGrQG

Does anyone know how to track this, because doing it manually and looking at the code does not work at all for me.

+3
source share
3 answers

The cell is 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 ​​statically assign a width (55) and a height (5) in MultiCell


Additionally:

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

You can see that the unit is the default value for the program / class PDF_UNIT


Then the font size is set using

$pdf->SetFont('times', '', 10);

(or just use SetFontSize for size only)

+1
source

TCPDF ( , ) getStringHeight() , Multicell(). , getNumLines() . http://www.tcpdf.org .

+3

, ...

** , ( ) ...

. capitol M (M - char), ..)

<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 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 not clear what 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'>&nbsp;</div>";

echo $outStr;
?> 
</body></html>
0
source

Source: https://habr.com/ru/post/1784208/