【问题标题】:FPDF align text LEFT, Center and RightFPDF 对齐文本左、中和右
【发布时间】:2015-07-27 07:02:24
【问题描述】:

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

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' ); 
} 

当我输出我的 PDF 文件时,center text 会自动对齐。 看起来是这样的:

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

【问题讨论】:

    标签: php pdf fpdf


    【解决方案1】:

    如果您将 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' ); 
        } 
    }
    

    【讨论】:

    • 什么是$thislMargin 来自哪里? FPDF 没有这样的属性。
    • Footer方法需要在FPDF的扩展类中实现。 FPDF has此属性。
    • 什么是 lMargin?得到它的错误。请在此处提及要使用的内容。
    • @RupaliPemare:请参阅我之前的评论以回答您的问题。我也相应地更新了我的答案。
    【解决方案2】:

    虽然 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');
    

    【讨论】:

      【解决方案3】:

      好像有这个参数,所以('R'右对齐,'C'居中):

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

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

      【讨论】:

        猜你喜欢
        • 2015-10-07
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 2015-04-24
        • 2014-12-12
        • 1970-01-01
        • 2012-10-13
        • 1970-01-01
        相关资源
        最近更新 更多