Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 564
RanZilber Avatar asked Mar 22 '12 10:03

RanZilber


People also ask

Why will my PDF files not combine?

Remember that Adobe Acrobat is unable to combine PDF files if the total file size is larger than 2 GB. To overcome this limit, we suggest using tools like Soda PDF in order to merge or split your PDF files. Soda PDF has an Online or Desktop app, both of which are easy to use for combining multiple files into one.

How do I merge two PDF documents with alternate pages?

To combine them, just click the Alternate & Mix button. In case the files were not uploaded in the right order, drag and drop to switch them. The odd pages PDF file is should be on the left and even pages PDF file on the right.

How do I combine locked PDF files?

If PDF is secured with User Level password It is suggested to use PDF Split & Merge Tool to merge PDF files that are having user-level password security. You only need to enter the password to merge any number of PDF files with the software.


1 Answers

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
like image 141
Chris Lercher Avatar answered Oct 27 '22 14:10

Chris Lercher