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...
\advance\malcount by 1%
% 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...
\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...
\fi
% Test all the pages from the input PDF file...
\ifnum\malcount<\pdflastximagepages\repeat
\end{document}