Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

pdf

tcpdf

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.

like image 404
raptorandy Avatar asked Oct 20 '25 14:10

raptorandy


2 Answers

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.

like image 94
Difster Avatar answered Oct 23 '25 05:10

Difster


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

$this->CurOrientation

e.g.

if($this->CurOrientation=="L") {
    // code for landscape pages
} else {
    // code for portrait pages
}
like image 32
sherwoor Avatar answered Oct 23 '25 03:10

sherwoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!