简体   繁体   中英

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.

    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"]);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM