php - 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.




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.




Inside your header & footer you'll have access to the orientation using



if($this->CurOrientation=="L") {
    // code for landscape pages
} else {
    // code for portrait pages

People are also looking for solutions to the problem: php - Laravel Collections: Shift Value *and* Key?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.