2011-01-08 162 views
3

我試圖通過TCPDF的代碼來了解它如何計算要呈現的文本的高度,但對於我而言,處理時不需要詢問太多。獲取文本高度以瞭解填充高度,如TCPDF

我想知道的是:在例子5的PDF中,http://www.tcpdf.org/examples/example_005.pdf給出了一個黃色的背景。我猜測,在基本層面上,它首先用這種填充顏色繪製一個框,然後添加文本,那麼調用什麼方法來獲取文本的高度以知道要填充的框的高度?

我可以從示例代碼中看到MultiCell()是入口點,但不清楚它調用什麼方法來獲取文本的高度。我粘貼了MultiCell()代碼在這個引擎收錄

http://pastebin.com/A1niGrQG

任何人知道如何追蹤這一點,因爲做手工,並希望通過代碼不工作對我來說。

+0

您是否查看了該特定示例頁面的代碼? http://www.tcpdf.org/examples/example_005.phps – CarpeNoctumDC 2011-01-08 02:57:50

+0

@CarpeNoctumDC示例代碼顯示MultiCell是入口點,但是當您查看MultiCell時,不清楚文本高度的計算方式。我可以將代碼粘貼到那裏。 – sami 2011-01-08 03:03:38

回答

1

該單元正被繪製由多單元: 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]) 

因此,大家可以看到,前兩個值被靜態地分配的寬度(55)和MultiCell的高度(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僅適用於尺寸)

0

就像我在上一次合作中所說的一個非常簡單的概念驗證mment我的其他答案...

**您需要使用等寬字體和行高等於你的文字高度(即或修改行的高度,而不是文字高度的代碼)相當簡單的修補程序。 ..

你還必須弄清楚你aproximate等寬寬度..做最好的辦法是使用國會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> 
3

TCPDF(至少最新版本)包括方法getStringHeight()以獲得使用Multicell()方法打印簡單文本字符串所需的估計高度。 此外,getNumLines()方法爲您提供估計的行數。 請查閱http://www.tcpdf組織的源代碼文檔以獲取更多信息。