Last active
February 14, 2023 13:48
-
-
Save kou1okada/2fd93c889c89d8646a2cb5892285dd1e to your computer and use it in GitHub Desktop.
pdfunbindbook.sh - unbind a book that all pages are double-page except covers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# pdfunbind.sh - unbind double-page PDF. | |
# Copyright (c) 2018 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT license. | |
# | |
# Reference: | |
# * [1] superuser / [Linux-based tool to chop PDFs into multiple pages [closed]](https://superuser.com/questions/191373/linux-based-tool-to-chop-pdfs-into-multiple-pages) | |
(( 5 <= DEBUG )) && set -x | |
function cleanup () | |
{ | |
rm "$WORK"*.pdf 2>/dev/null | |
trap 0 1 2 3 15 | |
} | |
function pagesize () | |
{ | |
pdfinfo "$1" | awk '/^Page size:/ {print $3,$5}' | |
} | |
function usage () | |
{ | |
cat <<-EOD | |
Usage: ${0##*/} [options] PDFFILE | |
Unbind double-page PDF. | |
Output file is \${PDFFILE%.*}_unbound.pdf | |
Options: | |
-m,--mode={book|2up} | |
Select mode (default: $OPT_MODE_DEFAULT). | |
book : All pages are double-page except covers. | |
2up : All pages are double-page. | |
EOD | |
} | |
OPT_MODE_DEFAULT=book | |
while (( 0 < $# )); do | |
case "$1" in | |
-h|--help) | |
OPT_HELP="$1" | |
shift | |
;; | |
-m*|--mode|--mode=*) | |
if [[ "$1" =~ ^(-m|--mode=)(book|2up)$ ]]; then | |
OPT_MODE="${BASH_REMATCH[1]}" | |
shift | |
elif [[ "$1 $2" =~ ^(-m |--mode )(book|2up)$ ]]; then | |
OPT_MODE="$2" | |
shift 2 | |
else | |
echo "Error: invalid option: $1 $2" | |
exit 1 | |
fi | |
;; | |
-*) | |
echo "Error: unknown option: $1" | |
exit 1 | |
;; | |
*) | |
ARGS+=( "$1" ) | |
shift | |
;; | |
esac | |
done | |
set -- "${ARGS[@]}" | |
if (( $# <= 0 )) || [ -n "$OPT_HELP" ]; then | |
usage | |
exit | |
fi | |
EL="\e[K" | |
PAGES=$(pdfinfo "$1" | awk '/^Pages:/{print $2}') | |
WORK="$(type pdftk | grep -q snap && mktemp -up. || mktemp -u)" | |
: ${OPT_MODE:=$OPT_MODE_DEFAULT} | |
case "$OPT_MODE" in | |
2up) COVER=0;; | |
book) COVER=1;; | |
esac | |
trap cleanup 0 1 2 3 15 | |
for i in `seq 1 $PAGES`; do | |
echo -en "\rSplitting: page $i" | |
pdftk "$1" cat $i output "$WORK"$(printf %03d $i).pdf | |
done | |
echo -e "\r${EL}Splitting: done" | |
for i in `seq $(( 1 + COVER )) $(( PAGES - COVER ))`; do | |
echo -en "\rUnbinding book: page $i" | |
PAGESIZE=( $( pagesize "$WORK"$(printf %03d $i).pdf ) ) | |
W="${PAGESIZE[0]}" | |
H="${PAGESIZE[1]}" | |
#BEGIN from reference [1] | |
Wh="$( echo "$W * 0.5" | bc -l | cut -d . -f1 )" | |
Whtt="$( echo "$W * 5" | bc -l | cut -d . -f1 )" | |
Htt="$( echo "$H * 10" | bc -l | cut -d . -f1 )" | |
gs -q -o "$WORK"$(printf %03d $i)L.pdf -sDEVICE=pdfwrite -g"$Whtt"x"$Htt" -c "<</PageOffset [0 0]>> setpagedevice" -f "$WORK"$(printf %03d $i).pdf | |
gs -q -o "$WORK"$(printf %03d $i)R.pdf -sDEVICE=pdfwrite -g"$Whtt"x"$Htt" -c "<</PageOffset [-$Wh 0]>> setpagedevice" -f "$WORK"$(printf %03d $i).pdf | |
#END from reference [1] | |
rm "$WORK"$(printf %03d $i).pdf | |
done | |
echo -e "\r${EL}Unbinding book: done" | |
echo -en "\rMerging: " | |
pdftk "$WORK"*.pdf cat output "${1%.*}_unbound.pdf" | |
echo "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment