[Tex/LaTex] includepdf landscape with scaling

landscaperotatingscale

I have a bunch of pdfs in a folder, some landscape, some portrait (so far none that are a mix, but it's possible)
I have a script to add the required \includepdf statements for each pdf file.
This generated tex file is then included in my main tex file.

I want the pdfs to appear at about 90% scale and for landscape pages to be rotated. So, I have the following

\includepdf[scale=0.9, frame, pages=-, pagecommand=\thispagestyle{pdffooter}]{"my file".pdf}

This gets the portrait pages in as I want them, but the landscape ones come out like this and still need rotating.

lanscape page.

I've tried various options to auto rotate the landscape pages but it seems they don't work in conjunction with the scale option. Since I am using a script to insert these '\includepdf' statements, I don't know in advance which pages need to be rotated.

Any ideas?

Best Answer

The following code is a bit of a kludge as it uses \includegraphics for determining the dimensions of a page before printing the document with \includepdf. Also, it does not handle a mix of portrait and landscape pages in one document.

\documentclass{article}
\usepackage{pdfpages}
\usepackage{graphicx}       % We need that for determining PDF dimensions

\newsavebox{\temp}
\newlength{\tempwidth}
\newlength{\tempheight}

\newcommand{\addpdf}[1]{%
    \sbox{\temp}{\includegraphics{#1}}%
    \setlength{\tempwidth}{\widthof{\usebox{\temp}}}%
    \setlength{\tempheight}{\heightof{\usebox{\temp}}}%

    \ifthenelse{\tempwidth > \tempheight}
        {\includepdf[fitpaper, templatesize={210mm}{297mm},rotateoversize=true, frame=true, scale=0.83, landscape]{#1}}
        {\includepdf[fitpaper, templatesize={210mm}{297mm},rotateoversize=true, frame=true, scale=0.83]{#1}}%
}


\begin{document}
\addpdf{example-image-a4.pdf}
\addpdf{example-image-a4-landscape.pdf}
\end{document}

Hope that helps!