2011-01-20 118 views
4

我如何讓這個生成的文本出現在頁面的中心。如何在FPDF中居中文本?

生成= $_POST方法...所以我不知道輸入文本將會有多長。我需要有一個預先確定的中心參數。

任何想法?也許是這樣的:

MultiCell(0,$height,"text",0,'C') ? 

回答

13

通常是$pdf->Cell(0, $height, "text", 0, 0, 'C');,但如果你在一個頁眉或頁腳功能做它的$this->Cell(0, $height, "text", 0, 0, 'C')。如果你在function()調用中這樣做,不要忘記將$height聲明爲全局變量。

1
$pdf->Text($mid_x-$pdf->GetStringWidth($text)/2,$y,$text); 
5

謝謝taur!這個工作對我來說:

$mid_x = 135; // the middle of the "PDF screen", fixed by now. 
$text = $userFullName; 
$pdf_file->Text($mid_x - ($pdf_file->GetStringWidth($text)/2), 102, $text); 
+3

就可以計算出文檔的中心:`$ mid_x = $ pdf_file-> W/2;` – 2015-07-16 16:47:58

2

這對於工作,你

MultiCell(0,$height,'You can<P ALIGN="center">center a line</P>',0,'C') 
+3

歡迎堆棧溢出!請不要僅發佈一段代碼,請解釋爲什麼此代碼可解決問題。沒有解釋,這不是一個答案。 – Artemix 2012-11-22 11:25:35