#!/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" if (a >= 594 && a <= 596 && b >= 419 && b <= 421 ) print "a5" if (a >= 594 && a <= 596 && b >= 841 && b <= 843 ) print "a4" if (a >= 841 && a <= 843 && b >= 594 && b <= 596 ) print "a4" if (a >= 841 && a <= 843 && b >= 1189 && b <= 1191) print "a3" if (a >= 1189 && a <= 1191 && b >= 841 && b <= 843 ) print "a3" }') 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 pdfjam --paper a4paper --nup '2x1' --landscape --outfile $recto -- "$output" 1 "$output" 1 if test $pages -eq 2; then pdfjam --paper a4paper --nup '2x1' --landscape --outfile $verso -- "$output" 2 "$output" 2 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