12

How can I get height and width of a document in 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);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
chenio
  • 592
  • 3
  • 11
  • 27

4 Answers4

49

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:

$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'); 
Ross McLellan
  • 1,872
  • 1
  • 15
  • 19
15

Update: November 2017

Nowadasy, you can simply call GetPageWidth and GetPageHeight methods.

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

$pdf->GetPageWidth();  // Width of Current Page
$pdf->GetPageHeight(); // Height of Current Page
Community
  • 1
  • 1
Ilario Pierbattista
  • 3,175
  • 2
  • 31
  • 41
8

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

}
hendr1x
  • 1,470
  • 1
  • 14
  • 23
1

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.

Mchl
  • 61,444
  • 9
  • 118
  • 120