# Merge PDFs, fix page numbering and compress

Say there is a book that has been split into several PDFs. I want to do two things:

1. Merge the PDFs and reduce the size of the resulting PDF.
2. Fix the page numbering.

(1) can be done with pdftk or Ghostscript. For me, Ghostscript creates considerably smaller PDFs and is the only program (of the two) available on my PC.

Example:

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -q -sOutputFile=output.pdf input_1.pdf input_2.pdf [...] input_n.pdf


Why is (2) a problem at all? Because different books start counting page numbers on different pages whereas your PDF reader starts counting at page 1 if you don't do anything. This can be fixed with an empty document, the package pdfpages, \setcounter, and \pagenumbering.

Example:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref}
\begin{document}
\pagenumbering{Roman}
\setcounter{page}{4}
\includepdf[pages=-]{input_1_with_TOC.pdf}
\pagenumbering{arabic}
\includepdf[pages=-]{input_2.pdf}
\includepdf[pages=-]{input_3.pdf}
% ...
\end{document}


My problem is now: I want to fix the page numbering and create a small PDF but calling pdflatex and then Ghostscript creates a PDF that is considerably bigger than a PDF that has been merged with Ghostscript.

How do I create PDFs as small as with (1) while having (2)?

EDIT: Forgot to add hyperref to the LaTeX code.

-
Is TeX really the best tool for this job? There are PDF libraries for many scripted languages that could renumber pages –  Matthew Leingang May 1 '13 at 21:04
If you are dealing with the pdf outputs only, there is no feasible way to modify the page numbers of the pagination. From the pdf's point of view they are just digit (or character!) sequences that happen to be positioned at some peculiar place of the page. –  Hagen von Eitzen May 1 '13 at 21:04
@HagenvonEitzen: There are the page numbers in the text ("on the paper") and there are the page numbers that are shown by your PDF reader. I want to change the latter. –  Throwaway 1352 May 1 '13 at 22:32
GhostScript likely reduces the size of the PDFs because it reduces the resolution of included bitmap images. Do you really want that? –  Martin Schröder May 3 '13 at 11:20
@Martin Schröder: Yes, I want to reduce the resolution. Otherwise I would have used Ghostscript with -dPDFSETTINGS=/preprint or only pdfpages. –  Throwaway 1352 May 3 '13 at 14:25

1. Merge with Ghostscript.
2. Use pdfpages on the Ghostscript output like this:
\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}
\pagenumbering{Roman}
\setcounter{page}{4}