Skip to content

Instantly share code, notes, and snippets.

@jacobkahn
Created March 2, 2017 21:48
Show Gist options
  • Save jacobkahn/f85e17d3c50e156cb73c6d8279eda809 to your computer and use it in GitHub Desktop.
Save jacobkahn/f85e17d3c50e156cb73c6d8279eda809 to your computer and use it in GitHub Desktop.
Crops a PDF printout with multiple pages and aggregates into a single page.
import copy
from pyPdf import PdfFileWriter, PdfFileReader
input1 = PdfFileReader(file("in.pdf", "rb"))
output = PdfFileWriter()
numPages = input1.getNumPages()
for i in range(numPages):
masterPage = input1.getPage(i)
aggPageCollection = []
for j in range(0, 3):
pageCollection = []
for q in range(0, 3):
page = copy.copy(masterPage)
xCoordsDerived = (30 + 250 * j, 28 + 192 * q)
yCoordsDerived = (258 + 255 * j, 200 + 192 * q)
page.cropBox.lowerLeft = xCoordsDerived
page.cropBox.upperRight = yCoordsDerived
pageCollection.append(page)
aggPageCollection.append(pageCollection)
for pageList in aggPageCollection:
output.addPage(pageList[2])
for pageList in aggPageCollection:
output.addPage(pageList[1])
for pageList in aggPageCollection:
output.addPage(pageList[0])
outputStream = file("out.pdf", "wb")
output.write(outputStream)
outputStream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment