问题描述
我有一份拼版文件:n
纸上有 4 × n
A4 页.我将它们放入滚筒图像扫描仪并收到一个 2 × n
分页的 PDF 文档 (A3).
I have an imposed document: there are 4 × n
A4 pages on the n
sheets. I put them into a roller image scanner and receive one 2 × n
paged PDF document (A3).
如果,比如说,n = 3
,那么我的 PDF 中有以下 A3 页面序列:
If, say, n = 3
, then I've got the following sequence of A3 pages in my PDF:
- 第一页:原始文档的第
12
页(左侧)和第1
页 - 第二页:原始文档的p.
2
和p.11
- 第三页:p.
10
和 p.3
... - ……依此类推,直到……
- 第六页:原始文档的p.
6
和p.7
- page one: page
12
(on the left) and page1
of the original document - page two: p.
2
and p.11
of the original document - page three: p.
10
and p.3
… - … and so on until…
- page six: p.
6
and p.7
of the original document
问题:如何在一个 A4 格式的 PDF 文件中重建页面的原始顺序?IE.我想这样做:
Question: how can I reconstruct the original sequence of pages in one PDF file of the A4 format? I.e. I want to do this:
--A3-- --A4--
[12| 1] [1]
[ 2|11] [2]
[10| 3] ⇒ [3]
… …
[ 6| 7] [6]
[7]
…
[12]
在 linux 中,对于这种情况,我通常使用 pdftk
或 pdftops
之类的控制台实用程序,但我不知道如何将它们用于我当前的目的.
In linux I usually use pdftk
or pdftops
-like console utilities for this kind of cases, but I cannot figure out how to use them for my current purpose.
推荐答案
过了一会儿我发现 这个线程 并稍微调整了代码:
After a while I found this thread and tuned the code a bit:
import copy
import sys
import math
import pyPdf
def split_pages(src, dst):
src_f = file(src, 'r+b')
dst_f = file(dst, 'w+b')
input_PDF = pyPdf.PdfFileReader(src_f)
num_pages = input_PDF.getNumPages()
first_half, second_half = [], []
for i in range(num_pages):
p = input_PDF.getPage(i)
q = copy.copy(p)
q.mediaBox = copy.copy(p.mediaBox)
x1, x2 = p.mediaBox.lowerLeft
x3, x4 = p.mediaBox.upperRight
x1, x2 = math.floor(x1), math.floor(x2)
x3, x4 = math.floor(x3), math.floor(x4)
x5, x6 = math.floor(x3/2), math.floor(x4/2)
if x3 > x4:
# horizontal
p.mediaBox.upperRight = (x5, x4)
p.mediaBox.lowerLeft = (x1, x2)
q.mediaBox.upperRight = (x3, x4)
q.mediaBox.lowerLeft = (x5, x2)
else:
# vertical
p.mediaBox.upperRight = (x3, x4)
p.mediaBox.lowerLeft = (x1, x6)
q.mediaBox.upperRight = (x3, x6)
q.mediaBox.lowerLeft = (x1, x2)
if i in range(1,num_pages+1,2):
first_half += [p]
second_half += [q]
else:
first_half += [q]
second_half += [p]
output = pyPdf.PdfFileWriter()
for page in first_half + second_half[::-1]:
output.addPage(page)
output.write(dst_f)
src_f.close()
dst_f.close()
if len(sys.argv) < 3:
print("\nusage:\n$ python reverse_impose.py input.pdf output.pdf")
sys.exit()
input_file = sys.argv[1]
output_file = sys.argv[2]
split_pages(input_file,output_file)
请参阅此要点.
这篇关于反向 PDF 拼版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!