alfredopacino
alfredopacino

Reputation: 3241

Get page width and height of a pdf

I searched for a while, there is a simple way to get the page dimensions of a PDF (of each page)?

I'm not clear if TCPDF or FPDF allows that.

Upvotes: 2

Views: 4867

Answers (2)

NeoTechni
NeoTechni

Reputation: 179

    function toPixels($value){
            //pt=point=0.352777778 mm, mm=millimeter=2.8346456675057350125948125904915 points, cm=centimeter=28.346456675057350125948125904915 points, in=inch=72 points=25.4mm
            switch(PDF_UNIT){//http://www.unitconversion.org/unit_converter/typography.html
                case "pt": return $value * 1.328352013;
                case "mm": return $value * 3.779527559;
                case "in": return $value * 96;
                case "cm": return $value * 37.795275591;
            }
            return "TEST";
        }
    
    $dimensions = [//PDF_UNIT defaults to millimeters
        "margins"   => $pdf->GetMargins(),
        "width"     => $pdf->getPageWidth(),
        "height"    => $pdf->getPageHeight(),
    ];
    $dimensions["width"] -= $dimensions["margins"]["left"] + $dimensions["margins"]["right"];
    $dimensions["height"] -= $dimensions["margins"]["top"] + $dimensions["margins"]["bottom"];
    $dimensions["width_pixels"] = toPixels($dimensions["width"]);
    $dimensions["height_pixels"] = toPixels($dimensions["height"]);

Upvotes: 1

Christian Felix
Christian Felix

Reputation: 673

You are in charge of setting the required Page dimension while creating an pdf object. For example the Fpdf sets per default an A4 Format, you may also adapt it to your current requirements by

 new FPDF('P','your Unit [mm]',[width,height])

Those are your dimensions values which apply to every page of your pdf document.

Furhermore you may also set it for each of your page separatelly:

AddPage([string orientation [, mixed size [, int rotation]]]

And least but not last, there are alse the concept of margins, where you may draw your content.

Upvotes: 0

Related Questions