Skip to content

Instantly share code, notes, and snippets.

@dukedougal
Last active August 29, 2015 13:56
Show Gist options
  • Save dukedougal/9340170 to your computer and use it in GitHub Desktop.
Save dukedougal/9340170 to your computer and use it in GitHub Desktop.
pdfinfo http://linux.about.com/library/cmd/blcmdl1_pdfinfo.htm
pdfjoin
pdfcrop
pdfcrop2 http://code.google.com/p/pdfcrop2/wiki/Usage
Crop with PyPDF
http://code.activestate.com/recipes/576837-crop-pdf-file-with-pypdf/
Cropping
To crop a PDF with PDFCrop:
pdfcrop original.pdf cropped.pdf
But there is a new problem: the cropped PDF has a different pagesize compared to the original one. We should resize the cropped PDF to its original pagesize. This is easy with Ghostscript:
gs -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dQUIET -sPAPERSIZE=a4 -dPDFFitPage -sOutputFile=resized.pdf cropped.pdf
In the above example, the pagesize of original PDF is assumed to A4. And the -dPDFFitPage option make sure the cropped PDF is resized instead of expanded.
#!/usr/bin/python
#
from pyPdf import PdfFileWriter, PdfFileReader
input1 = PdfFileReader(file("in.pdf", "rb"))
output = PdfFileWriter()
numPages = input1.getNumPages()
print "document has %s pages." % numPages
for i in range(numPages):
page = input1.getPage(i)
print page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y()
page.trimBox.lowerLeft = (25, 25)
page.trimBox.upperRight = (225, 225)
page.cropBox.lowerLeft = (50, 50)
page.cropBox.upperRight = (200, 200)
output.addPage(page)
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