3

我正在尝试通过TCPDF的代码来了解它如何计算要呈现的文本的高度,但是我不问就无法处理。

我想知道的是:在示例 5 http://www.tcpdf.org/examples/example_005.pdf的 PDF 中,它为单元格提供了黄色背景。我猜在基本层面上,它首先用这种填充颜色绘制一个框,然后添加文本,那么它调用什么方法来获取文本的高度以知道要填充的框的高度?

我可以从示例代码中看到这MultiCell()是入口点,但不清楚它调用什么方法来获取文本的高度。MultiCell()我在这个 pastebin 中粘贴了代码

http://pastebin.com/A1niGrQG

任何人都知道如何追踪这一点,因为手工完成并查看代码对我来说根本不起作用。

4

3 回答 3

5

TCPDF(至少是最新版本)包括使用该方法getStringHeight()获取打印简单文本字符串所需的估计高度的Multicell()方法。此外,该getNumLines()方法为您提供了估计的行数。查看http://www.tcpdf.org上的源代码文档以获取更多信息。

于 2011-09-27T07:21:53.300 回答
1

单元格由 MultiCell 绘制: http ://www.tcpdf.org/examples/example_005.phps

$pdf->MultiCell(55, 5, '[LEFT] '.$txt, 1, 'L', 1, 0, '', '', true);

并来自:http ://api.joomla.org/com-tecnick-tcpdf/TCPPDF.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]) 

如您所见,前两个值静态地为 MultiCell 分配宽度 (55) 和高度 (5)


此外:

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

可以看到测量单位是程序/类默认的 PDF_UNIT


然后设置字体大小

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

(或仅使用 SetFontSize 作为大小)

于 2011-01-08T03:04:00.737 回答
0

就像我在对其他答案的最后评论中所说的话的一个非常简单的概念证明......

** 您需要使用等宽字体和等于您的文本高度的行高(或者修改行高而不是文本高度的代码)相当简单的修复...

你还必须弄清楚你的近似等宽宽度.. 最好的办法是使用国会大厦 M(M 是最宽的字符 - 所以等宽字符设置为这个宽度..)

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

echo $outStr;
?> 
</body></html>
于 2011-01-08T06:01:43.290 回答