Skip to content

Instantly share code, notes, and snippets.

@bohwaz
Last active July 29, 2023 11:08
Show Gist options
  • Save bohwaz/f817920e61db03361be6c2a2373ccb0c to your computer and use it in GitHub Desktop.
Save bohwaz/f817920e61db03361be6c2a2373ccb0c to your computer and use it in GitHub Desktop.
pd2cbz.sh - Convert a PDF file (eg. a comic) to a CBZ file
#!/bin/bash
which mutool &> /dev/null || (echo "mutool is not installed" && exit 1)
which convert &> /dev/null || (echo "imagemagick is not installed" && exit 1)
which zip &> /dev/null || (echo "zip is not installed" && exit 1)
if [ "$1" = "" ]
then
echo "Usage: $0 File.pdf"
exit
fi
TEMP=`mktemp -d "${TMPDIR:-/tmp}"/pdf2cbz.XXXX`
echo "Extracting images..."
mutool convert -F png -O height=1920 -o "${TEMP}/page-%04d.png" "$1"
echo "Converting"
find "${TEMP}" -type f -name "*.png" -exec echo -n . \; -exec convert -quality 92 -strip "{}" "{}.jpg" \;
echo ""
echo "Zipping..."
zip -q -j "${1%.pdf}.cbz" "${TEMP}"/*.jpg
rm -rf "${TEMP}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment