I have an imposed document that are 4-up on a standard pdf. I need to reverse impose them back to 1-up pdf's. I have looked at the solution here. This works perfectly to split the pdf in half. But I'm new to python scripts and can't quite figure out how to modify this for my needs. my PDF is organized as:
[1|3] [1]
[2|4] [2]
----- [3]
[5|6] ⇒ [4]
[7|8] [5]
---- [6]...
If possible, would be great to have it process only a range of page numbers, but that's only icing. I've found a java application with gui called [briss here][1] to achieve what I need, but it would be great to learn a little python to be able to automate this task.
So I've gotten as far as this being able to output what I need, but I'm unsure how to setup the script to accept page number range
import copy
import math
from PyPDF2 import PdfFileReader, PdfFileWriter
import argparse
def split_pages2(src, dst):
src_f = file(src, 'r+b')
dst_f = file(dst, 'w+b')
input = PdfFileReader(src_f)
output = PdfFileWriter()
for i in range(input.getNumPages()):
# make two copies of the input page
pp = input.getPage(i)
p = copy.copy(pp)
q = copy.copy(pp)
r = copy.copy(pp)
s = copy.copy(pp)
# the new media boxes are the previous crop boxes
p.mediaBox = copy.copy(p.cropBox)
q.mediaBox = copy.copy(p.cropBox)
#x1, x2 = p.mediaBox.lowerLeft
x1, x2 = 72, 71.5
#x3, x4 = p.mediaBox.upperRight
x3, x4 = 540.7, 727
x1, x2 = math.floor(x1), math.floor(x2)
x3, x4 = math.floor(x3), math.floor(x4)
#x5, x6 = x1+math.floor((x3-x1)/2), x2+math.floor((x4-x2)/2)
x5, x6 = 306, 396
# vertical
p.mediaBox.upperRight = (x5, x6)
p.mediaBox.lowerLeft = (x1, x2)
q.mediaBox.upperRight = (x5, x4)
q.mediaBox.lowerLeft = (x1, x6)
r.mediaBox.upperRight = (x3, x6)
r.mediaBox.lowerLeft = (x5, x2)
s.mediaBox.upperRight = (x3, x4)
s.mediaBox.lowerLeft = (x5, x6)
p.artBox = p.mediaBox
p.bleedBox = p.mediaBox
p.cropBox = p.mediaBox
q.artBox = q.mediaBox
q.bleedBox = q.mediaBox
q.cropBox = q.mediaBox
r.artBox = r.mediaBox
r.bleedBox = r.mediaBox
r.cropBox = r.mediaBox
s.artBox = s.mediaBox
s.bleedBox = s.mediaBox
s.cropBox = s.mediaBox
output.addPage(q)
output.addPage(p)
output.addPage(s)
output.addPage(r)
output.write(dst_f)
src_f.close()
dst_f.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=("Split up pdf from 4-up "
"to single page"))
parser.add_argument("src", help="Source file")
parser.add_argument("dst", help="Destination file")
args = parser.parse_args()
split_pages2(args.src, args.dst)