Created
February 16, 2013 12:48
-
-
Save unot/4966808 to your computer and use it in GitHub Desktop.
PDF file to CBZ file for Kobo Glo
This file contains 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
#!/bin/bash -ex | |
# pdftocbz.sh | |
# Written by Takashi UNO | |
# require: poppler, ImageMagick | |
if [ $# -ne 1 ]; then | |
echo "USAGE: `basename $0` hogehoge.pdf" | |
exit 1 | |
fi | |
for cmd in awk zip pdftoppm convert ; do | |
if [ ! -x "`which $cmd`" ]; then | |
echo "command $cmd is not found." | |
exit 1 | |
fi | |
done | |
if [ ! -f "$1" ]; then | |
echo "$1: file not exist." | |
exit 1 | |
fi | |
SRC="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" | |
TMPDIR="/var/tmp/`basename "$1" .pdf`" | |
# TMPDIR="${1%.pdf}" | |
trap 'rm -rf "${TMPDIR}"' EXIT | |
mkdir "${TMPDIR}" && cd "${TMPDIR}" | |
pdftoppm -r 300 -png "$SRC" image | |
TITLE=`pdfinfo "$SRC" | grep Title | awk '{print $2}'` | |
AUTHOR=`pdfinfo "$SRC" | grep Author | awk '{print $2}'` | |
TOP=4 | |
BOTTOM=96 | |
RIGHT=4 | |
LEFT=4 | |
SIZE="758x1024" | |
BORDER=20 | |
for INPUT in image*.png; do | |
convert ${INPUT} \ | |
-chop ${LEFT}x${TOP} \ | |
-gravity SouthEast -chop ${RIGHT}x${BOTTOM} \ | |
-gravity NorthWest \ | |
-fuzz 50% -trim \ | |
-bordercolor white -border ${BORDER} \ | |
-type grayscale \ | |
-geometry ${SIZE} \ | |
-sharpen 5 \ | |
-depth 4 ${INPUT%.png}_resized.png | |
done | |
test -x `which optipng` && optipng *_resized.png | |
if [ "a" = "a${AUTHOR}" -a "t" = "t${TITLE}" ]; then | |
zip -jqr "${SRC%.pdf}.cbz" *_resized.png | |
else | |
zip -jqr "`dirname "$SRC"`/[${AUTHOR}] ${TITLE}.cbz" *_resized.png | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment