Splitting 2up PDFs back to 1up

Splitting 2up PDFs back to 1up

Sometimes you’ll get a document scanned in that has 2 pages per scanned ‘page’. It is really nice to have them put back to one page per ‘page’, undoing the 2up process. The below is adapted from here and here.

First, install the pyPdf python library by issuing python setup.py build and sudo python setup.py install in the unzipped library directory.

The below code will then undo the 2up. It may need to be adjusted for landscape or portrait originals and if odd and even pages have the same or differing layouts.

#!/usr/bin/env python
# USAGE: ./un2up.py < in.pdf > out.pdf
# It takes a 2up page and returns it to 1up
# Adapted from http://stackoverflow.com/questions/7047656/why-my-code-not-correctly-split-every-page-in-a-scanned-pdf
# and http://unix.stackexchange.com/questions/12482/split-pages-in-pdf

import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for i in range(input.getNumPages()):
    p = input.getPage(i)
    q = copy.copy(p)

    bl = p.mediaBox.lowerLeft
    ur = p.mediaBox.upperRight

    print >> sys.stderr, 'splitting page',i
    print >> sys.stderr, '\tlowerLeft:',p.mediaBox.lowerLeft
    print >> sys.stderr, '\tupperRight:',p.mediaBox.upperRight

# altered for landscaped 2up of portrait original
    p.mediaBox.upperRight = (ur[0]/2, ur[1])
    p.mediaBox.lowerLeft = bl

# altered for landscaped 2up of portrait original
    q.mediaBox.upperRight = ur
    q.mediaBox.lowerLeft = (ur[0]/2, bl[1])

# This bit allows adjustment if each 2up page alternates which side is odd and even

    if i%2==0: