如何在FPDF中居中文本?

NOR*_*ORM 5 php pdf-generation fpdf

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

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

有任何想法吗?也许是这样的:

MultiCell(0,$height,"text",0,'C') ?
Run Code Online (Sandbox Code Playgroud)

小智 15

通常它是, $pdf->Cell(0, $height, "text", 0, 0, 'C');但如果你在页眉或页脚功能中这样做它$this->Cell(0, $height, "text", 0, 0, 'C').$height如果你在function()调用中这样做,不要忘记声明为全局.


小智 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);
Run Code Online (Sandbox Code Playgroud)

  • 您可以计算文档的中心:`$ mid_x = $ pdf_file-> w/2;` (4认同)