Update: Thanks to stardt whose script works! The pdf is a page of another one. I tried the script on the other one, and it also correctly spit each pdf page, but the order of page numbers is sometimes right and sometimes wrong. For example, in page 25-28 of the pdf file, the printed page numbers are 14, 15, 17, are 16. I was wondering why? The entire pdf can be downloaded from http://download304.mediafire.com/u6ewhjt77lzg/bgf8uzvxatckycn/3.pdf
Original: I have a scanned pdf, where two paper pages sit side by side in a pdf page. I would like to split the pdf page into two, with the original left half becoming the earlier of the two new pdf pages. The pdf looks like .
Here is my Python script named un2up
inspired by Gilles:
#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
q = copy.copy(p)
(w, h) = p.mediaBox.upperRight
p.mediaBox.upperLeft = (0, h/2)
p.mediaBox.upperRight = (w, h/2)
p.mediaBox.lowerRight = (w, 0)
p.mediaBox.lowerLeft = (0, 0)
q.mediaBox.upperLeft = (0, h)
q.mediaBox.upperRight = (w, h)
q.mediaBox.lowerRight = (w, h/2)
q.mediaBox.lowerLeft = (0, h/2)
output.addPage(q)
output.addPage(p)
output.write(sys.stdout)
I tried the script on a pdf in terminal with command being un2up < page.pdf > out.pdf
, but the output out.pdf
is not correctly split.
I also checked the values of variables w
and h
, the output of p.mediaBox.upperRight
, and they are 514
and 1224
which don't look right based on their actual ratio.
The file can be downloaded from http://download851.mediafire.com/bdr4sv7v5nzg/raci13ct5w4c86j/page.pdf.