Skip to content

Instantly share code, notes, and snippets.

@ravijo
Last active March 29, 2024 14:32
Show Gist options
  • Save ravijo/011c6699bb721b95199b80beab01ba43 to your computer and use it in GitHub Desktop.
Save ravijo/011c6699bb721b95199b80beab01ba43 to your computer and use it in GitHub Desktop.
This shell script converts all PDF files to JPG files
#!/bin/bash
# author: ravi joshi
# date: 19 mar 2020
# this shell script converts all PDF files to JPG files
# it walks recursively inside the given directory
# requirement: it uses ImageMagick command line tool for PDF to JPG convertion.
# check here (https://github.com/ravijo/image-video-editing#install-imagemagick-on-ubuntu-1404)
# for more information
if [ $# -eq 0 ]; then
echo "input directory is not provided"
exit 1
fi
# location to input directory
in_dir="$1"
# src: https://unix.stackexchange.com/a/494146
walk_dir () {
for path_name in "$1"/*; do
if [ -d "$path_name" ]; then
walk_dir "$path_name"
elif [ -e "$path_name" ]; then
case "$path_name" in *.PDF|*.pdf)
printf 'Reading file: %s\n' "$path_name"
file_name=$(basename "$path_name")
dir_name=$(dirname "$path_name")
name_only="${file_name%.*}"
save_name="${dir_name}/${name_only}.jpg"
# printf '%s\n' "$path_name"
# printf '%s\n' "$save_name"
# src: https://github.com/ravijo/image-video-editing#covert-pdf-to-jpeg-high-quality
convert -density 150 "$path_name" -quality 90 -background white -alpha remove "$save_name"
# rm "$path_name" # uncomment this line if you want to delete the original PDF file
esac
fi
done
}
walk_dir "$in_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment