What on Earth?
This post describes one method of merging the pages of two pdf documents. That is, creating an ouput file where page 1 of the output is the result of merging page 1 of each of the inputs, page 2 of the output is the result of merging page 2 of each of the inputs and so on.
The reason that I wanted to do this was so that I could combine the publishing capabilities of Scribus with the music notation of MuseScore (which seems pretty good to me, apart from a few minor UI annoyances and the fact that it crashes every now and then). Scribus wouldn’t import pdfs nicely, so I came up with this approach instead.
What You’ll Need
My solution to this problem is presented below as a python script. Before running the script, you will need to install pyPdf. On Linux, download and unzip the source, then run python setup.py install
. You will of course also need Python installed.
How It’s Done
Create a new file called merge.py
and paste the following code into it. I hereby release the following python code into the public domain.
!/usr/bin/python
from pyPdf import PdfFileWriter, PdfFileReader
import sys
def main(file1, file2, outfile):
output = PdfFileWriter()
input1 = PdfFileReader(file(file1, "rb"))
input2 = PdfFileReader(file(file2, "rb"))
p1 = len(input1.pages)
p2 = len(input2.pages)
for i in xrange(min(p1, p2)):
print 'Merging page %d' % (i,)
p = input1.getPage(i)
p.mergePage(input2.getPage(i))
output.addPage(p)
if p1 > p2:
inp = input1
else:
inp = input2
for i in xrange(min(p1, p2), max(p1, p2)):
print 'Adding page %d' % (i,)
output.addPage(inp.getPage(i))
output.write(file(outfile, 'wb'))
if __name__ == '__main__':
main(*sys.argv[1:4])
Now to merge two pdf files, just type:
python merge.py input1.pdf input2.pdf output.pdf