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
How to Center Text in FPDF?
提问by NORM
How can I have this generated text appear centered in the page.
我怎样才能让这个生成的文本出现在页面的中心。
Generated = $_POST
method ... so I don't know how long will the text in input be. I need to have a pre-determined center parameter somehow.
Generated = $_POST
method ... 所以我不知道输入中的文本会有多长时间。我需要以某种方式有一个预先确定的中心参数。
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 $height
as 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);