scripts / livret /
ccb9f31 4 years ago
1 contributor
32 lines | 1.247kb
#!/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}')
test $pages -ne 4 -o -z "$size" && exit	

recto=$(mktemp -d /dev/shm/XXXXXXXX)
verso=$(mktemp -d /dev/shm/XXXXXXXX)
output=$(mktemp --dry-run /dev/shm/XXXXXXXX.pdf)

if test $size = "a3"; then
    size="a4"
    gs -o $output -sDEVICE=pdfwrite -sPAPERSIZE=$size -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.6 "$pdf"
else
    cp "$pdf" $output
fi

booksize="a$((${size//[^45]}-1))"
pdfjam --paper ${booksize}paper --nup 2x1 --landscape --outfile $recto "$output" 1 "$output" 4
pdfjam --paper ${booksize}paper --nup 2x1 --landscape --outfile $verso "$output" 2 "$output" 3
pdftk A=$(find $recto -name "*pdf") B=$(find $verso -name "*pdf") cat A B output "${pdf%/*}/livret-${pdf##*/}"
rm -fr $recto $verso $output