php 如何获取使用 FPDF 生成的文档的宽度和高度

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

How do I get the width and height of a doc generated with FPDF

phpfpdf

提问by chenio

How can I get height and width of a document in FPDF.

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

For example, I've next line:

例如,我有下一行:

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

But, I want to do something like:

但是,我想做一些类似的事情:

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

采纳答案by Mchl

note: read Ross' McLellan's answer below

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

As far as I remember you can't do it with vanilla FPDF. You can either extend it to have a method that would return this value for you, or just store the width as a public property of fpdf object.

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

回答by Ross McLellan

Needed to do this myself so was just checking the most recent version of FPDF and it looks like the width & height are already available as public properties. So for anyone looking for the same info:

需要自己做这件事,所以只是检查最新版本的 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'); 

回答by Ilario Pierbattista

Update: November 2017

更新:2017 年 11 月

Nowadasy, you can simply call GetPageWidthand GetPageHeightmethods.

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

$pdf = new FPDF(); 
$pdf->addPage("P", "A4");

$pdf->GetPageWidth();  // Width of Current Page
$pdf->GetPageHeight(); // Height of Current Page

回答by hendr1x

Encase someone needs to get the width taking margins into consideration...

包住某人需要考虑到边距的宽度......

class FPDF_EXTEND extends FPDF
{

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

}