php FPDF 将文本左、中、右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30258066/
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 21:39:54  来源:igfitidea点击:

FPDF align text LEFT, Center and Right

phppdffpdf

提问by Jan Slabon

I've have three cells and i'am trying to align the text to Left, Center and Right.

我有三个单元格,我正在尝试将文本与左、中和右对齐。

function Footer() 
{ 
    $this->SetY( -15 ); 


    $this->SetFont( 'Arial', '', 10 ); 

    $this->Cell(0,10,'Left text',0,0,'L');

    $this->Cell(0,10,'Center text:',0,0,'C');

    $this->Cell( 0, 10, 'Right text', 0, 0, 'R' ); 
} 

When I ouput my PDF file, center textget automatically aligned right. This is how it looks:

当我输出我的 PDF 文件时,center text自动对齐。这是它的外观:

enter image description here

在此处输入图片说明

Can somebody tell me what I'm doing wrong here and how I can fix this issue?

有人可以告诉我我在这里做错了什么以及如何解决这个问题?

回答by Jan Slabon

The new position after a Cell call will be set to the right of each cell if you set the ln-parameter of the Cell method to 0. You have to reset the x-coordinate before the last 2 Cell calls:

如果将 Cell 方法的 ln 参数设置为 0,则 Cell 调用后的新位置将设置在每个单元格的右侧。 您必须在最后 2 个 Cell 调用之前重置 x 坐标:

class Pdf extends FPDF {
    ...

    function Footer() 
    { 
        $this->SetY( -15 ); 

        $this->SetFont( 'Arial', '', 10 ); 

        $this->Cell(0,10,'Left text',0,0,'L');
        $this->SetX($this->lMargin);
        $this->Cell(0,10,'Center text:',0,0,'C');
        $this->SetX($this->lMargin);
        $this->Cell( 0, 10, 'Right text', 0, 0, 'R' ); 
    } 
}

回答by Francisco Lozano

Though Jan Slabon's answer was really good I still had issues with the center not being exactly centered on my page, maybe we have different versions of the library and that's what accounts for the slight differences, for instance he uses lMargin and on some versions that's not available. Anyway, the way it worked for me is this:

尽管 Jan Slabon 的回答非常好,但我仍然遇到中心未完全以我的页面为中心的问题,也许我们有不同版本的库,这就是造成细微差异的原因,例如他使用 lMargin 并且在某些版本上不是可用的。无论如何,它对我有用的方式是这样的:

        $pdf = new tFPDF\PDF();
        //it helps out to add margin to the document first
        $pdf->setMargins(23, 44, 11.7);
        $pdf->AddPage();
        //this was a special font I used
        $pdf->AddFont('FuturaMed','','AIGFutura-Medium.ttf',true);
        $pdf->SetFont('FuturaMed','',16);

        $nombre = "NAME OF PERSON";
        $apellido = "LASTNAME OF PERSON";

        $pos = 10;
        //adding XY as well helped me, for some reaons without it again it wasn't entirely centered
        $pdf->SetXY(0, 10);

        //with SetX I use numbers instead of lMargin, and I also use half of the size I added as margin for the page when I did SetMargins
        $pdf->SetX(11.5);
        $pdf->Cell(0,$pos,$nombre,0,0,'C');

        $pdf->SetX(11.5);
        //$pdf->SetFont('FuturaMed','',12);
        $pos = $pos + 10;
        $pdf->Cell(0,$pos,$apellido,0,0,'C');
        $pdf->Output('example.pdf', 'F');

回答by Vishnja

Seems that there is a parameter for this, so ('R' for right-aligning, 'C' for centering):

似乎有一个参数,所以('R' 用于右对齐,'C' 用于居中):

$pdf->Cell(0, 10, "Some text", 0, true, 'R');

$pdf->Cell(0, 10, "Some text", 0, true, 'R');

will right align text. Also, notice that the first parameter ('width') is zero, so cell has 100% width.

将右对齐文本。另外,请注意第一个参数 ('width') 为零,因此单元格的宽度为 100%。