php 如何在 FPDF 中居中文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4742794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:12:55  来源:igfitidea点击:

How to Center Text in FPDF?

phppdf-generationfpdf

提问by NORM

How can I have this generated text appear centered in the page.

我怎样才能让这个生成的文本出现在页面的中心。

Generated = $_POSTmethod ... so I don't know how long will the text in input be. I need to have a pre-determined center parameter somehow.

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

Any ideas? Maybe like this:

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

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

回答by BlackMagic

Normally it's $pdf->Cell(0, $height, "text", 0, 0, 'C');but if you're doing it in a Header or Footer function it's $this->Cell(0, $height, "text", 0, 0, 'C'). Don't forget to declare $heightas a global if you're doing this in a function() call.

通常 $pdf->Cell(0, $height, "text", 0, 0, 'C');是这样,但如果您在 Header 或 Footer 函数中执行此操作,则它是$this->Cell(0, $height, "text", 0, 0, 'C'). $height如果您在 function() 调用中执行此操作,请不要忘记声明为全局变量。

回答by C. Jaacks

Thanks taur! This works for me:

谢谢牛头!这对我有用:

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

回答by Balram Singh

This may work for you

这可能对你有用

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

回答by taur

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