如何在 FPDF 中将文本居中?

问题描述 投票:0回答:6

如何让生成的文本显示在页面中央。

Generated =

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

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

MultiCell(0,$height,"text",0,'C') ?
php pdf-generation fpdf
6个回答
16
投票

通常是

$pdf->Cell(0, $height, "text", 0, 0, 'C');
,但如果您在页眉或页脚函数中执行此操作,则为
$this->Cell(0, $height, "text", 0, 0, 'C')
。如果您在 function() 调用中执行此操作,请不要忘记将
$height
声明为全局变量。


6
投票

谢谢牛牛!这对我有用:

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

4
投票

这可能对你有用

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

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

0
投票
            global $title;

            
            // Calculate width of text and position
            $w = $this->GetStringWidth($title)+6;
            $this->SetX((210-$w)/2);
            
        
            // Line break
            $this->Ln(10);

         
            $title = 'YOUR TEXT HERE';
            $this->SetTitle($title);
            $this->SetTextColor(0,0, 0);
            $this->SetFont('Arial','',17);
            $this->SetX(((210-$w)/4),(-50));
            $this->Cell(10, 20, $title);

0
投票

这对我有用

$mid_x = $pdf->GetPageWidth() / 2;
$text = $userName;
$pdf->Text($mid_x - ($pdf->GetStringWidth($text) / 2),{height}, $text);
© www.soinside.com 2019 - 2024. All rights reserved.