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

后端 未结 8 1134
-上瘾入骨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:12

    The code by @Chris Lercher in https://stackoverflow.com/a/12761103/1369181 did not quite work for me. I do not know whether that is because I am working on Cygwin/mintty. Also, I have to use qpdf instead of pdftk. Here is the code that has worked for me:

    #!/bin/bash
    
    for f in *.pdf; do
      npages=$(pdfinfo "$f"|grep 'Pages:'|sed 's/[^0-9]*//g')
      modulo=$(($npages %2))
      if [ $modulo -eq 1 ]; then
        qpdf --empty --pages "$f" "path/to/blank.pdf" -- "aligned_$f"
      else
        cp "$f" "aligned_$f"
      fi
    done
    

    Now, all "aligned_" files have even page numbers, and I can join them using qpdf (thanks to https://stackoverflow.com/a/51080927):

    qpdf --verbose --empty --pages aligned_* -- all.pdf
    

    And here the useful code from https://unix.stackexchange.com/a/272878 that I have used for creating the blank page:

    echo "" | ps2pdf -sPAPERSIZE=a4 - blank.pdf
    

提交回复
热议问题