如何在FPDF中居中文本?

时间:2011-01-20 01:59:30

标签: php pdf-generation fpdf

如何让生成的文本显示在页面的中心位置。

Generated = $_POST方法......所以我不知道输入中的文本会有多长。我需要以某种方式预先确定一个中心参数。

有什么想法吗?也许是这样的:

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

4 个答案:

答案 0 :(得分:15)

通常它是$pdf->Cell(0, $height, "text", 0, 0, 'C');但是如果你在页眉或页脚功能中执行它$this->Cell(0, $height, "text", 0, 0, 'C')。如果您在function()调用中执行此操作,请不要忘记将$height声明为全局。

答案 1 :(得分:6)

谢谢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);

答案 2 :(得分:3)

这可能适合你

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

答案 3 :(得分:1)

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