How can I merge pdf files so that each file begins on an odd page number?

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)

The first step is to produce a pdf file with an empty page. You can do this easily with a lot of programs (LibreOffice/OpenOffice, inkscape, (La)TeX, scribus, etc.)

Then just include this empty page where needed:

pdftk A.pdf empty_page.pdf B.pdf output result.pdf 

If you want to do this automatically with a script, you can use e.g. pdftk file.pdf dump_data | grep NumberOfPages | egrep -o '[0-9]*' to extract the page count.

Tags:

Pdf

Merge