Search code examples
pdfghostscriptpdftk

Ghostscript script or other, to interleave odd and even pages from two multipage pdf files


My scanner has a paper feed but no duplex scanning. Thus, if I scan a multi-page document that is printed on both sides of the sheet, I will end up with two pdf files, one containing all odd pages (the odd-page pdf file), the other all even pages (the even-page pdf file).

I feel it should be possible to have a Ghostscript script that merges the two files in such a way that each page from the even-page pdf file will be added as every other page to the odd-page pdf file.

Would anyone know how to do that?

Preferably, the script would take arguments, so that the first argument specifies the output file, the second argument the odd-page pdf file and the third argument the even-page pdf file.


Solution

  • So, if I rightly understand, you have two pdf files

    • odd.pdf
    • even.pdf

    and you need to INTERLEAVE these pages from these two multipage pdf files: (odd, even, odd... and so on...)

    I wrote some time ago, for same needs, a script I attach, it is INTERACTIVE, meaning it asks for arguments, if you prefer a NON INTERACTIVE SCRIPT, I can modify it

    it only needs PDFTK

    #!/bin/bash
    #script able to interleave the pages of two pdf files, saving the result in a new pdf file. Useful for any use, specially to mount parallel text books
    echo "enter the name (with extension) of first PDF"
    read filename1
    echo "enter the name (with extension) of second PDF"
    read filename2
    pages1="`pdftk $filename1 dump_data output |grep Pages|cut -f2 -d :`"
    pages2="`pdftk $filename2 dump_data output |grep Pages|cut -f2 -d :`"
    
    if [ $pages1 -gt $pages2 ]
        then
        pagesincr="$(echo "scale=0; $pages2+1" |bc -l)"
    echo "$filename1 has $pages1 pages"
    echo "$filename2 has $pages2 pages"
    
    rule="$(for x in $(seq 1 $pages2); do echo -n "A$x B$x ";  done; for x in $(seq $pagesincr $pages1); do echo -n "A$x ";done)"
    
    
        echo $rule
    
            elif
            [ $pages2 -gt $pages1 ]
                then
        pagesincr="$(echo "scale=0; $pages1+1" |bc -l)"
    
    echo "$filename1 has $pages1 pages"
    echo "$filename2 has $pages2 pages"
    
    rule="$(for x in $(seq 1 $pages1); do echo -n "A$x B$x ";  done; for x in $(seq $pagesincr $pages2); do echo -n "B$x ";done)"
    
    
        echo $rule
                    else
    echo "$filename1 has $pages1 pages"
    echo "$filename2 has $pages2 pages"
    
    rule="$(for ((a=1, b=1; a <= $pages1, b <= $pages2 ; a++, b++)); do echo -n "A$a B$b "; done)"
    
    echo $rule
    fi
    
    pdftk A=$filename1 B=$filename2 cat $rule output interleaved.pdf
    echo "file created!"
    exit 0