# Automatic inserting of portrait and landscape pdf documents with pdfpages

I have a problem that is bothering me for quite a while. I would like to insert a couple of pdfs automatically as attachments to a document. The user copies a bunch of pdfs in a folder, where he finds a .bat file that will create the attachments.tex document from all files automatically. The process is automated pretty far, the .bat creates a \section{} element for every pdf, too, checks for signs incompatible with Latex and removes them, such as \$, & or spaces and so on.

Because it is doing all by itself already, I don't want to bother the user with the parameters of the inlcudepdf command. So, landscape documents should be included on landscape paper automatically.

I could do that by using the command

\includepdf[fitpaper]{attachment.pdf}


Unfortunately, there are often pretty large drawings (A3,A2 paper). So I would like to have them scaled down to A4. Using the command

\includepdf[templatesize={211mm}{298mm},rotateoversize]{attachment.pdf}


I was able to have them all scaled down to A4 and automatically rotated, which is close to what I was looking for and the best solution so far.

What I cannot find is a scale down to A4, without a rotation for oversize pages. The parameter 'landscape=true' needs to be set manually for all landscape documents at the moment, which is tedious. Basically what I would need is parameter for pdfpages like

landscapeoversize


Unfortunately, this does not exist. Is there any workaround with the pdfpages package itself which I just did not think of?

Here is a solution. We use pdftex primitives \pdfximage and \pdflastximagepages

\documentclass{article}
\usepackage{pdfpages}

\newcounter{mtpdfpage}
\newsavebox{\mtsavebox}

\begin{document}
\pdfximage{attachment.pdf}
\setcounter{mtpdfpage}{0}
\loop
\stepcounter{mtpdfpage}%
\sbox{\mtsavebox}{\includegraphics[page=\themtpdfpage]{attachment.pdf}}%
\ifdim\ht\mtsavebox<\wd\mtsavebox
\includepdf[pages=\themtpdfpage, landscape]{attachment.pdf}%
\else
\includepdf[pages=\themtpdfpage]{attachment.pdf}%
\fi
\ifnum\value{mtpdfpage}<\pdflastximagepages
\repeat
\end{document}

• Wow, thank you! I see that I have still years ahead of me to become nerdy enough to get this stuff myself, but it is exactly what I needed! Mar 18, 2016 at 8:46
• Unfortunately, I encountered a problem with the solution: It does rotate and scale the pages as expected, but it drops the last page of every document longer than one page. Apr 11, 2016 at 11:53
• I figured out that it is because of the last \ifnum command. It should compare mtpdfpage less or equal to pdflastximagepages. I couldn't find the correct way to specify less or equal, so I inserted a second counter mtpdfpageless, which starts with the value 0. In the end, I just compare mtpdfpageless<\pdflastximagepages. It works, but it's not a beautiful solution. Is there a better way? Apr 11, 2016 at 12:02
• @Tilman sorry, may bad. I edited the answer. Apr 11, 2016 at 15:16