Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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'd like to print it double sided, such that two files will not be on the same page.

Therefore it I'd the merging file to be aligned such that every file will begin in odd page and a blank page will be added if the next place to write is an even page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
642 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...