Skip to content

Instantly share code, notes, and snippets.

@eush77
Created June 8, 2017 19:28
Show Gist options
  • Save eush77/862f99f27fd67b9505d0125f1573c3b6 to your computer and use it in GitHub Desktop.
Save eush77/862f99f27fd67b9505d0125f1573c3b6 to your computer and use it in GitHub Desktop.
Decolumnize PDFs for ebook readers
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: t; tab-width: 4; -*-
set -euo pipefail
IFS=$'\n'
function help {
cat <<-EOF
Usage: $0 <input.pdf> <output-file-or-directory>
Cut file horizontally, then vertically, then crop margins.
Make it readable on a 6" ebook reader.
EOF
}
# decol <input.pdf> <output.pdf>
function decol {
input="$1"
output="$2"
if [[ -e "$output" ]]; then
read -rp "File '$output' exists. Overwrite? (y/n) "
if [[ "${REPLY::1}" != "y" ]]; then
exit
fi
fi
tempx=$(tempfile --suffix=.pdf)
tempy=$(tempfile --suffix=.pdf)
trap "rm $tempx $tempy" EXIT
mutool poster -x2 "$input" "$tempx"
mutool poster -y2 "$tempx" "$tempy"
pdfcrop "$tempy" "$output"
}
if [[ "$1" = "--help" || "$#" -ne 2 ]]; then
help
exit
fi
if [[ -d "$2" ]]; then
decol "$1" "$2/$(basename $1)"
else
decol "$1" "$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment