# Using pdfpages with mixed landscape / portrait pdfs

I am using pdfpages to include PDFs in my LaTeX document.

Some of these pdfs have both landscape and portrait pages, and I won't necessarily know in advance where these occur.

Is there a way to get pdfpages to handle this mixed content? At the moment, I am just including everything as portrait ([landscape=false]), which does produce a workable document but with the landscape pages rotated and scaled down.

Alternatively, is there a different solution than pdfpages?

MWE where one.pdf is a (hypothetical) PDF with mixed landscape and portrait pages:

\documentclass[12pt,english]{article}
\usepackage{pdfpages}

\begin{document}

This is the text in the master file.

\includepdf[pages=-]{one}

\end{document}


Let our first step is to prepare such a book (book.tex).

%! *latex book.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{pdflscape}
\begin{document}
\font\malfont=cmbx10 at 500pt \malfont
\def\insertpage{\mbox{}\vfil\hfil\thepage}
\insertpage
\begin{landscape}\insertpage\end{landscape}
\insertpage
\begin{landscape}\insertpage\end{landscape}
\end{document}


The fastest way is probably to use the pdfpages package, http://www.ctan.org/pkg/pdfpages, with an option rotateoversize switched to true. This is an example and its result.

%! *latex book-transform1.tex
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages={-},fitpaper,rotateoversize]{book.pdf}
\end{document}


The only problem would be if we want to rotate (or ornament, typeset in, sign, watermark them etc.) landscape pages the other way around. This is an improved version. We are testing every single page if it was typeset in a portrait/landscape mode. We will discover that by measuring its page width and page height in a virtual box before typesetting.

The terminal is informing us about the findings.

Page 1, portrait, 597.50682pt, height 845.04504pt, depth 0.0pt.
Page 2, landscape, 845.04504pt, height 597.50682pt, depth 0.0pt.
Page 3, portrait, 597.50682pt, height 845.04504pt, depth 0.0pt.
Page 4, landscape, 845.04504pt, height 597.50682pt, depth 0.0pt.

I enclose an example and a preview of our efforts.

%! *latex book-transform2.tex
\batchmode
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\def\malfile{book.pdf}
\pdfximage{\malfile}

\begin{document}
\newcount\malcount \malcount=0 % This is a page counter...
\newbox\malbox % This is a virtual box...

\loop % Let's test all the pages one by one...
% Measure a single page virtually...
\setbox\malbox=\hbox{\includegraphics[page={\the\malcount}]{\malfile}}%
% Send the information to the terminal...
\scrollmode
\message{Page \the\malcount, \ifnum\wd\malbox<\ht\malbox portrait\else landscape\fi, \the\wd\malbox, height \the\ht\malbox, depth \the\dp\malbox.}%
\batchmode

% Testing and inserting that particular single page into a document...