#!/bin/bash pdf="$1" size=$(pdfinfo "$pdf" | awk '/^Page size:/{ a = $3 b = $5 if (a >= 419 && a <= 421 && b >= 594 && b <= 596 ) print "a5 portrait" if (a >= 594 && a <= 596 && b >= 419 && b <= 421 ) print "a5 paysage" if (a >= 594 && a <= 596 && b >= 841 && b <= 843 ) print "a4 portrait" if (a >= 841 && a <= 843 && b >= 594 && b <= 596 ) print "a4 paysage" if (a >= 841 && a <= 843 && b >= 1189 && b <= 1191) print "a3 portrait" if (a >= 1189 && a <= 1191 && b >= 841 && b <= 843 ) print "a3 paysage" }') pages=$(pdfinfo "$pdf" | awk '/Pages:/{print $2}') output=$(mktemp --dry-run /dev/shm/XXXXXXXX.pdf) recto=$(mktemp --directory /dev/shm/XXXXXXXX) verso=$(mktemp --directory /dev/shm/XXXXXXXX) if test "${size% *}" != "a5"; then gs -o $output -sDEVICE=pdfwrite -sPAPERSIZE=a5 -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.6 "$pdf" else cp "$pdf" $output fi if test "${size#* }" = "portrait"; then pdfjam --paper a4paper --nup '2x1' --landscape --outfile $recto -- "$output" 1 "$output" 1 else pdfjam --paper a4paper --nup '1x2' --no-landscape --outfile $recto -- "$output" 1 "$output" 1 fi if test $pages -eq 2; then if test "${size#* }" = "portrait"; then pdfjam --paper a4paper --nup '2x1' --landscape --outfile $verso -- "$output" 2 "$output" 2 else pdfjam --paper a4paper --nup '1x2' --no-landscape --outfile $verso -- "$output" 2 "$output" 2 fi pdftk A=$(find $recto -name "*pdf") B=$(find $verso -name "*pdf") cat A B output "$output" else mv $(find $recto -name "*pdf") $output fi mv $output "${pdf%/*}/massicotable-${pdf##*/}" rm -fr $recto $verso $output