Skip to content

Instantly share code, notes, and snippets.

@brozkeff
Last active February 2, 2024 14:09
Show Gist options
  • Save brozkeff/b061b69800d3a08b088dfb89ca044fb5 to your computer and use it in GitHub Desktop.
Save brozkeff/b061b69800d3a08b088dfb89ca044fb5 to your computer and use it in GitHub Desktop.
PDF splitter of scanned documents using mupdf-tools (mutool poster)
#!/bin/bash
SCRIPT_NAME="PDF splitter of scanned documents"
VERSION_NUMBER="v2024-02-02"
AUTHOR="Martin Brozkeff Malec - https://brozkeff.net"
LICENSE="MIT (https://opensource.org/license/mit/)"
# Shared on https://gist.github.com/brozkeff/b061b69800d3a08b088dfb89ca044fb5
echo "${SCRIPT_NAME} ${VERSION_NUMBER}"
echo "Author: ${AUTHOR}"
echo "License: ${LICENSE}"
# This script splits scanned PDF pages either vertically or horizontally down the middle,
# separating each page into two, and saves the result with a '-SPLIT' suffix.
# Usage: scriptname [-h | -v] input.pdf
split_direction="-x" # Default to vertical split
while getopts ":hv" option; do
case $option in
h) split_direction="-y" ;; # Horizontal split
v) split_direction="-x" ;; # Vertical split (default)
\?) echo "Invalid option: -$OPTARG" >&2
exit 1 ;;
esac
done
shift $((OPTIND -1))
# Determine and echo the chosen split direction
if [ "$split_direction" = "-x" ]; then
echo "Selected split direction: Vertical"
else
echo "Selected split direction: Horizontal"
fi
# Check if a filename is provided
if [ -z "$1" ]; then
echo "No filename provided. Exiting..."
exit 1
fi
OUTPUT_FILE="${1%.pdf}-SPLIT.pdf"
# Check for required tool 'mutool'
if ! command -v mutool &> /dev/null; then
echo "This script requires 'mutool' to be installed."
echo "Attempting to install 'mupdf-tools'. Please provide your password if prompted."
sudo apt-get update
sudo apt-get install -y mupdf-tools
fi
# Directly split each page using mutool based on the specified direction
mutool poster $split_direction 2 "$1" "$OUTPUT_FILE"
echo "Splitting completed. Output saved to '${OUTPUT_FILE}'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment