[Tex/LaTex] 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:



This is the text in the master file.



Best Answer

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

%! *latex book.tex
\font\malfont=cmbx10 at 500pt \malfont

mwe book

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

mwe transformation 1

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

\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...
\advance\malcount by 1%
% Measure a single page virtually...
% Send the information to the terminal...
\message{Page \the\malcount, \ifnum\wd\malbox<\ht\malbox portrait\else landscape\fi, \the\wd\malbox, height \the\ht\malbox, depth \the\dp\malbox.}%

% Testing and inserting that particular single page into a document...
\ifnum\wd\malbox<\ht\malbox %Portrait; no changes please...
\includepdf[pages={\the\malcount}, fitpaper]{\malfile}
  \else % Landscape mode; rotate it, decorate it, draw something on it... :-)
\includepdf[pages={\the\malcount}, fitpaper, angle=270]{\malfile}% 90 degrees is the default value in pdfpages, 270 degrees is an experiment, if somebody is in need of it...

% Test all the pages from the input PDF file...


mwe transformation 2