Skip to content

Instantly share code, notes, and snippets.

@nhoriguchi
Created January 24, 2021 01:07
Show Gist options
  • Save nhoriguchi/7fcce4b97bdeecf6bf8f689ed41d75a6 to your computer and use it in GitHub Desktop.
Save nhoriguchi/7fcce4b97bdeecf6bf8f689ed41d75a6 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Usage
#
# - Take screenshot files and put them into a directory. You can't put
# unrelated file into the directory.
# - Then run "bash convert.sh <DIRECTORY_PATH>"
#
# Notes:
#
# - You need some image converter tools (convert, pdfcrop, gs)
# - Pages are orderer in timestamp order, so you should take screenshot
# in page order.
#
TARGETDIR=$1
SIZESPEC=$2
[ ! "$SIZESPEC" ] && SIZESPEC=1710x2158+1064+2
[ ! -d "$TARGETDIR" ] && echo "TARGETDIR not found." && exit 1
[ "$DEBUG" ] && set -x
if which convert > /dev/null ; then
echo "command 'convert' not found"
exit 1
fi
if which pdfcrop > /dev/null ; then
echo "command 'pdfcrop' not found"
exit 1
fi
if which gs > /dev/null ; then
echo "command 'gs' not found"
exit 1
fi
cd $TARGETDIR
rm tmp_*
NR_PAGES=$(ls -1 | grep png$ | grep -v tmp_ | wc -l)
if [ "$NR_PAGES" -eq 0 ] ; then
echo "no png files found"
exit 1
fi
i=1
files=
for line in $(ls -1tr | grep png$ | grep -v tmp_) ; do
cp "$line" tmp_$i.png
convert -crop $SIZESPEC "$line" tmp_$i.png
files="$files tmp_$i.png"
i=$[i+1]
done
convert $files tmp_output1.pdf || exit 1
pdfcrop --margins '0 0 0 0' tmp_output1.pdf tmp_output2.pdf || exit 1
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dGrayImageResolution=72 -dMonoImageResolution=72 -dColorImageResolution=72 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=tmp_output3.pdf tmp_output2.pdf || exit 1
mv tmp_output3.pdf output.pdf
rm tmp_*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment