Skip to content
Advertisement

TCPDF – How to detect in Header/Footer function if pdf page is in landscape or portrait mode?

I have a cutom page and header function in my TCPDF file. Since I use a different header/footer for portrait and landscape pages, I want to detect the orientation of the page and use the correct header/footer.

First I was using a switch with “this->page” to indicate which header/footer to use based on the page number. But sometimes the text on page on a portrait page is longer and it adds an additional page and I end up with a mess.

How can I detect if a pdf page is in landcape or portrait mode?

Something like this is what I want to do, if it is possible.

class MYPDF extends TCPDF {
    public function Header() {
        //Detect if the page is on landscape or portrait mode
        $pageOrientation = ????

        if(//page orientation is landscape){
            //use this custom header
        }
        else {
            //use this other custom header
        }        
    }
}

Thanks in advance.

Advertisement

Answer

I have a solution for you. Maybe it’s a little ugly but it will work. Since you are declaring the orientation of each page when you create the page, just set an $orientation variable for that page and then test for the value of $orientation when calling your function. Whatever the current value is going down the page, then it acts accordingly. If you want to get more sophisticated, you could keep an array of page numbers + orientation. That way you could act on a particular page number and/or loop through and do specific things to specific pages based on orientation.

Ugly but it would be effective.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement