FPDF对齐文本LEFT,Center和Right

6 php pdf fpdf

我有三个单元格,我试图将文本对齐左,中,右.

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

当我输出我的PDF文件时,center text自动对齐.这是它的样子:

在此输入图像描述

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

Jan*_*bon 11

如果将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' ); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是“$this”?“lMargin”从哪里来?FPDF没有这样的属性。 (2认同)

小智 5

虽然 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');
Run Code Online (Sandbox Code Playgroud)