Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

fpdf

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);
like image 259
chenio Avatar asked Jan 16 '12 15:01

chenio


People also ask

How do you find the height of multicell in Fpdf?

You can just use $pdf->GetY(); to get the curent y value. Then when you have printed all the text, you can use $pdf->GetY(); to get the height after each piece of text.

How do I insert an image in Fpdf?

Adding Image to PDF file by FPDF php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->Image('images/pdf-header. jpg',0,0); $pdf->Output(); ?> The ouput of above code is here . ( Show Output ) We can add position with height, width and link to above code.


3 Answers

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'); 
like image 174
Ross McLellan Avatar answered Sep 28 '22 08:09

Ross McLellan


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
like image 29
Ilario Pierbattista Avatar answered Sep 28 '22 08:09

Ilario Pierbattista


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

}
like image 32
hendr1x Avatar answered Sep 28 '22 06:09

hendr1x