Merge PDF Files – Start Each on Odd Page Number

mergepdf

I need to merge a few dozed pdfs, and i want all of the input pdfs to start on an odd page in the output pdf.

Example: A.pdf has 3 pages, B.pdf has 4 pages. I don't want my output to have 7 pages. What I want is an 8-page pdf in which pages 1-3 are from A.pdf, page 4 is empty, and pages 5-8 are from B.pdf. How can I do this?

I know about pdftk, but I didn't find such an option in the man page.

Best Answer

The PyPdf library makes this sort of things easy if you're willing to write a bit of Python. Save the code below in a script called pdf-cat-even (or whatever you like), make it executable (chmod +x pdf-cat-even), and run it as a filter (./pdf-cat-even a.pdf b.pdf >concatenated.pdf). You need pyPdf ≥1.13 for the addBlankPage method.

#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
output_page_number = 0
alignment = 2           # to align on even pages
for filename in sys.argv[1:]:
    # This code is executed for every file in turn
    input = PdfFileReader(open(filename))
    for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
        # This code is executed for every input page in turn
        output.addPage(p)
        output_page_number += 1
    while output_page_number % alignment != 0:
        output.addBlankPage()
        output_page_number += 1
output.write(sys.stdout)