3

TCPDFのコードを調べて、レンダリングされるテキストの高さを計算する方法を理解しようとしていますが、質問せずに処理するには多すぎます。

知りたいこと:例5のPDF http://www.tcpdf.org/examples/example_005.pdfでは、セルの背景が黄色になっています。基本的なレベルでは、最初にこの塗りつぶしの色でボックスを描画し、次にテキストを追加すると思います。テキストの高さを取得して、塗りつぶすボックスの高さを知るには、どのメソッドを呼び出しますか?

MultiCell()エントリポイントであるサンプルコードからはわかりますが、テキストの高さを取得するために呼び出すメソッドが何であるかは明確ではありません。MultiCell()このペーストビンにコードを貼り付けました

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/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]) 

ご覧のとおり、最初の 2 つの値は、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 に答える