【问题标题】:Reverse PDF imposition反向 PDF 拼版
【发布时间】:2019-01-29 02:28:57
【问题描述】:

我有一份拼版文件:n 表上有4 × n A4 页面。我将它们放入滚轮图像扫描仪并收到一份2 × n 分页 PDF 文档 (A3)。

如果说,n = 3,那么我的 PDF 中有以下 A3 页面序列:

  • 第一页:原始文档的页面12(左侧)和页面1
  • 第二页:原始文档的 p.2 和 p.11
  • 第三页:p.10 和 p.3...
  • ……以此类推,直到……
  • 第六页:原始文档的 p.6 和 p.7

问题:如何在一个 A4 格式的 PDF 文件中重建原始页面顺序? IE。我想这样做:

--A3--         --A4--
[12| 1]         [1]
[ 2|11]         [2]
[10| 3]    ⇒    [3]
   …             … 
[ 6| 7]         [6]
                [7]
                 … 
                [12]

在 linux 中,我通常使用 pdftkpdftops 之类的控制台实用程序来处理这种情况,但我不知道如何将它们用于我目前的目的。

【问题讨论】:

    标签: python linux pdf-generation pdftk imposition


    【解决方案1】:

    过了一会儿,我找到了this thread 并稍微调整了代码:

    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)
    

    看到这个gist

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多