Tuesday, November 6, 2007

Merge PDF documents with LaTeX

Sometimes, a single book comes in multiple PDF documents, one document for a chapter. I often want to merge these PDF document into one single document. There are several possibilites, e.g. using Adobe Acrobat. A less expensive and yet easy solution is to use LaTeX for that purpose. So I have created a simple LaTeX template in which I only have to add the names of the documents to be merged., that's it. Besides, several options are available such as scaling the documents on the fly. The work is done by the LaTex package "pdfpages", see pdfpages at www.ctan.org. The documents are included with \includepdf, the two lines above this command are used here for creating a table of content in the PDF.

%!TEX encoding = UTF-8 Unicode
% -------+---------+---------+---------+---------+---------+---------+---------+
\documentclass{book}
% -------+---------+---------+---------+---------+---------+---------+---------+
% Used Packages
% -------+---------+---------+---------+---------+---------+---------+---------+
\usepackage[pdftex,dvips]{graphicx}
\usepackage[english]{babel}
\usepackage[
 pdfhighlight=/O, colorlinks, linkcolor=black, urlcolor=black, citecolor=black,
 breaklinks, bookmarksopen,bookmarksopenlevel=1,linktocpage
] {hyperref}
\usepackage{fancyhdr}
\usepackage{pdfpages} % That does the trick!
% -------+---------+---------+---------+---------+---------+---------+---------+
% Header
% -------+---------+---------+---------+---------+---------+---------+---------+
\pagestyle{headings}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
%\fancyfoot[LE,RO]{\raisebox{-25mm}{\large\textsf{\thepage}}}
%\fancyfoot[RE,LO]{\raisebox{-25mm}{Merged Document}}
%\fancyfoot[CE,CO]{\raisebox{-25mm}{}}
\renewcommand{\headrulewidth}{0mm}
\renewcommand{\footrulewidth}{0mm}
% -------+---------+---------+---------+---------+---------+---------+---------+
% Settings for including the PDF documents,
% see: 
% http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages/pdfpages.pdf
% -------+---------+---------+---------+---------+---------+---------+---------+
\includepdfset{pages=-,nup=1x1, pagecommand={\mbox{}}}
% -------+---------+---------+---------+---------+---------+---------+---------+
% The Documents
% -------+---------+---------+---------+---------+---------+---------+---------+
\begin{document}

\phantomsection 
\addcontentsline{toc}{chapter}{Chapter 1}
\includepdf{file1.pdf}

\phantomsection 
\addcontentsline{toc}{chapter}{Chapter 2}
\includepdf{file2.pdf}

% ...

% -------+---------+---------+---------+---------+---------+---------+---------+
\end{document}

Post a Comment