Automatic merging of one page pdfs to two page pdfs

Viewed 63

I know there are tools like pdftk or convert or ghostscript. but my searches failing because all of the solutions are build for 2 pdf-files merged into 1 pdf-file.

But I have 30.000 PDF files and want to merge them down to 15.000 two sided PDF files. Because the first page is the front and the second page is the back. And I need them combined in one pdf with front and back.

They are numbered down starting with 00001.pdf and 00002.pdf and they need to combined to a new two pages pdf. And then next, 00003.pdf and 00004.pdf and so on. Any chance to build a command which automatically grabs the next 2 pdf and merge them?

Alternative, I thougth about a "size-break" every file is near 2,5MB big. A command to build only 5MB pdfs and then head over to the next file.

Thx for any suggestions.

P.S. I have all the files in .jpg, too.

1 Answers

In Bash, something like this:

#! /bin/bash

fileIndex=1

inputFormat="%05d.pdf"        # change the formats 
outputFormat="%05d+%05d.pdf"  # to support other name patterns

while true
do
    file1root=$((fileIndex++))
    file2root=$((fileIndex++))
    printf -v file1 "$inputFormat" "$file1root"
    printf -v file2 "$inputFormat" "$file2root"
    printf -v output "$outputFormat" "$file1root" "$file2root"
    [[ -f $file1 ]] && [[ -f $file2 ]] || break
    echo execute some command $file1 $file2 $output ####
done

where the #### line is replaced by a command that merges two PDF into one PDF

Related