Skip to content

Instantly share code, notes, and snippets.

@cantor
Created September 16, 2012 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cantor/3730785 to your computer and use it in GitHub Desktop.
Save cantor/3730785 to your computer and use it in GitHub Desktop.
This script inputs a pdf file and outputs a pdf file that is in booklet format. You will then be able to print the output file using duplex (both sides of paper) and 2 pages per side. The script is also capable of printing multiple signatures.
#!/bin/bash
# booklet.sh - This script inputs a pdf file and outputs a pdf file that is in booklet
# format. You will then be able to print the output file using duplex
# (both sides of paper) and 2 pages per side. The end result is a printed
# document that can be stapled in the middle and folded so that it may be
# viewed like a book. The script is also capable of printing multiple
# signatures.
#
# This script is licensed under the GPL license v3.
# See https://www.gnu.org/copyleft/gpl.html for more information.
#
# Basic Usage: sh booklet.sh file.pdf
#
# Note: you can also specify the crop with a second argv
# i.e., sh booklet.sh file.pdf 10
#
# Note: Again, this script assumes that you are going to print
# using duplex, 2 pages per side.
# Check for empty input and print Usage if empty.
case $1 in
"")
echo
echo "####Instructions####"
echo "# booklet.sh - A script that formats a pdf into a booklet."
echo "Usage: sh $0 file.pdf"
echo "# Note: you can also specify the crop with a second argv"
echo "sh $0 file.pdf 10"
echo "###################"
echo
exit
;;
*)
file=$1
case $2 in
"") #if no crop argv default is 10.
cropin=10
;;
*)
cropin=$2
;;
esac
;;
esac
#get number of pages of $file
pagenum=$(pdftk $file data_dump output |grep -i Num |awk '{print $2}')
# divisorslist n will output the divisors of n.
# $1=ssinglesigpagenum
function divisorlist() {
local divisors
local x=$1
local i=2 # possible divisor
local j=2
local k=1
local n=$(expr $x / 2) # top limit for divisor
divisors[1]=1
while [ $i -le $n ]
do
let k=$x%$i # residual
if [ $k -eq 0 ]
then
divisors[$j]=$i
let j=$j+1
fi
let i=$i+1
done
divisors[$j+1]=$1
echo "${divisors[@]}"
}
# convert file n m will convert file to a booklet
# of with signature of length n and crop of size m.
# The only function that actually does anything.
# $1=$file $2=$siglength $3=crop
function convert() {
local files=$1
local siglength=$2
local crop=$3
local filebase=$(basename $1 .pdf)
pdfcrop --margins $crop $files ${filebase}-crop.pdf
pdf2ps ${filebase}-crop.pdf
psbook -s $siglength ${filebase}-crop.ps >${filebase}-booklet.ps
rm -f ${filebase}-crop.pdf ${filebase}-booklet-crop.ps
echo "* Converting back to pdf ..."
ps2pdf ${filebase}-booklet.ps
rm -f ${filebase}-booklet.ps ${filebase}-crop.ps
}
# pagenum0mod4 file pagenum outputs page number that is 0mod4
# $1=$file $2=$pagenum
function pagenum0mod4() {
local files=$1
local pagenum=$2
local diffmod=$(($pagenum % 4))
local newpagenum=$pagenum
local pagenumadd=$((4 - $diffmod))
if [ "$diffmod" = 0 ]
then
newpagenum=$pagenum
else
newpagenum=$(($pagenum + $pagenumadd))
fi
echo $newpagenum
}
# where the magic happens.
# $1=$file $2=$newpagenum $3=cropin
function userinterface() {
local files=$1
local newpagenum=$2
local crop=$3
local switch=0
local singlesigpagenum=$(($newpagenum / 4))
local signaturechoices=($(divisorlist $singlesigpagenum))
# Ask for length of each signature
# and do stupidity check on input.
while [ $switch = 0 ]
do
echo
echo "Question: How many signature do you want? (If you do not know, 1 is recommended) 1 signature requires $singlesigpagenum pages for this document."
echo "[Choices are ${signaturechoices[@]}] or Ctrl+c to exit:"
read signum
for j in ${signaturechoices[@]}; do
case $signum in
$j)
switch=1;
break;
;;
*)
;;
esac
done
done
#siglength is length of each signature
local siglength=$(($newpagenum / $signum))
echo
echo "*Creating $signum signatures $siglength pages long..."
convert $file $siglength $crop
}
####################################
# main() - lets run this shit.
newpagenum=$(pagenum0mod4 $file $pagenum)
echo
echo -e "There are $pagenum pages in:"
echo "- $file"
echo "...."
echo "New document will have $newpagenum pages and will be cropped $cropin."
userinterface $file $(pagenum0mod4 $file $pagenum) $cropin
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment