Getting text height to know fill height as in TCPDF

17,673

Solution 1

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)

Solution 2

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.

Share:
17,673
sami
Author by

sami

Updated on June 30, 2022

Comments

  • sami
    sami almost 2 years

    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 code 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.