How can I merge PDF files (or PS if not possible) such that every file will begin in a odd page?

后端 未结 8 1126
-上瘾入骨i
-上瘾入骨i 2020-12-15 07:50

I am working on a UNIX system and I\'d like to merge thousands of PDF files into one file in order to print it. I don\'t know how many pages they are in advance.

I\'

8条回答
  •  一生所求
    2020-12-15 08:03

    Here's the solution I use (it's based on @Dingo's basic principle, but uses an easier approach for the PDF manipulation):

    First, I create a PDF file with a single blank page somewhere, e.g. in "/path/to/blank.pdf".

    Then, from the directory that contains all my pdf files, I run a little script that appends the blank.pdf file to each pdf with an odd page number:

    #!/bin/bash
    
    for f in *.pdf; do
      let npages=$(pdfinfo "$f"|grep 'Pages:'|awk '{print $2}')
      let modulo="($npages %2)"
      if [ $modulo -eq 1 ]; then
        pdftk "$f" "/path/to/blank.pdf" output "aligned_$f"
      else
        cp "$f" "aligned_$f"
      fi
    done
    

    Now, all "aligned_" files have even page numbers, and I can join them using

    pdftk aligned_*.pdf output result.pdf
    

提交回复
热议问题