Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

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

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...