【问题标题】:How do I get the width and height of a doc generated with FPDF如何获取使用 FPDF 生成的文档的宽度和高度
【发布时间】:2012-01-16 15:52:31
【问题描述】:

如何在 FPDF 中获取文档的高度和宽度。

例如,我有下一行:

$this->Cell(200,5,'ATHLETIC DE COLOMBIA S.A.',1,1,'C',1);

但是,我想做这样的事情:

// $x = width of page
$this->Cell($x,5,'ATHLETIC DE COLOMBIA S.A.',1,1,'C',1);

【问题讨论】:

    标签: php fpdf


    【解决方案1】:

    我需要自己做,所以只是检查了最新版本的 FPDF,看起来宽度和高度已经作为公共属性可用。所以对于任何寻找相同信息的人:

    $pdf = new FPDF(); 
    $pdf->addPage("P", "A4");
    
    $pdf -> w; // Width of Current Page
    $pdf -> h; // Height of Current Page
    
    $pdf -> Line(0, 0, $pdf -> w, $pdf -> h);
    $pdf -> Line($pdf -> w, 0, 0, $pdf -> h);
    
    $pdf->Output('mypdf.pdf', 'I'); 
    

    【讨论】:

    • 这是正确的答案,虽然这不在文档页面上。
    • 您必须在当前 FPDF 版本中将 publicity 更改为 public。
    • GetPageWidth() 现已推出
    • @hendr1x GetPageWidth() 在 v16 中不起作用
    【解决方案2】:

    更新:2017 年 11 月

    现在,您可以简单地调用 GetPageWidthGetPageHeight 方法。

    $pdf = new FPDF(); 
    $pdf->addPage("P", "A4");
    
    $pdf->GetPageWidth();  // Width of Current Page
    $pdf->GetPageHeight(); // Height of Current Page
    

    【讨论】:

    • GetPageWidth 返回什么测量值?像素?毫米?厘米?
    • 毫米、厘米、磅或英寸。默认为毫米。您可以在创建 FPDF 时指定另一个度量来更改它。
    【解决方案3】:

    装箱需要考虑边距的宽度...

    class FPDF_EXTEND extends FPDF
    {
    
        public function pageWidth()
        {
            $width = $this->w;
            $leftMargin = $this->lMargin;
            $rightMargin = $this->rMargin;
            return $width-$rightMargin-$leftMargin;
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      注意:阅读下面罗斯的麦克莱伦的回答

      据我记得你不能用香草 FPDF 做到这一点。您可以将其扩展为具有为您返回此值的方法,或者将宽度存储为 fpdf 对象的公共属性。

      【讨论】:

      • 宽度:$this->w 高度:$this->h
      猜你喜欢
      • 2015-12-15
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 2011-07-12
      • 2014-05-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多