Skip to content

Instantly share code, notes, and snippets.

@brozkeff
Created May 17, 2017 12:50
Show Gist options
  • Save brozkeff/9f1254d3a3b01953332c3d6d795797a2 to your computer and use it in GitHub Desktop.
Save brozkeff/9f1254d3a3b01953332c3d6d795797a2 to your computer and use it in GitHub Desktop.
Impose PDF - repeat the same page multiple times
#!/bin/bash
#
# imposepdf-repeat.sh
#
# Author: Martin Malec <[email protected]>
# Date : May 17, 2017
#
# Description: imposepdf-repeat.sh repeats one-page PDF multiple times
# on a single page. Requires bookletimposer installed.
#
# Usage : imposepdf-repeat.sh -r <repetitions> <inputfile.pdf>
# where <repetitions> is the number of repetitions of the page, valid values
# are currently 2 (2x1), 4 (2x2), 8 (4x2) or directly the [w]x[h]
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#####################################################
usage="$(basename "$0") [-h] [-r n] inputfile.pdf -- repeats one-page PDF multiple times on a single page
where:
-h show this help text
-r sets number of repetitions (2, 4, 8, or directly a matrix for bookletimposer, such as 4x4) (default: 2)"
repetitions=2
while getopts ':hr:' option; do
case "$option" in
h) echo "$usage"
exit
;;
r) repetitions=$OPTARG
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
hash bookletimposer 2>/dev/null || { echo >&2 "error: bookletimposer seems not to be installed or available in PATH Exiting, install it first (apt install bookletimposer in Debian/Ubuntu/Mint)."; exit 1; }
re='^[0-9]+$'
if ! [[ $repetitions =~ $re ]] ; then
echo "warning: Amount of repetitions is not a number, valid numbers are 2, 4 or 8, any other values are sent without check to bookletimposer" >&2
fi
if ! [[ -r "$1" ]]
then
echo "error: Input file $1 not found">&2
echo "$usage" >&2
exit 1
fi
case "$repetitions" in
"2")
layout="2x1"
bookletimposer -a -b -c -n -p "$layout" -o "${1%.*}"-"$repetitions"x.pdf "$1"
;;
"4")
layout="2x2"
bookletimposer -a -b -c -n -p "$layout" -o "${1%.*}"-"$repetitions"x.pdf "$1"
;;
"8")
layout="4x2"
bookletimposer -a -b -c -n -p "$layout" -o "${1%.*}"-"$repetitions"x.pdf "$1"
;;
*)
layout="$repetitions"
bookletimposer -a -b -c -n -p "$layout" -o "${1%.*}"-"$repetitions".pdf "$1"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment