Skip to content

Instantly share code, notes, and snippets.

@rslhdyt
Created April 18, 2023 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rslhdyt/bc70832a7c1b2e0344e065064bc5cfc3 to your computer and use it in GitHub Desktop.
Save rslhdyt/bc70832a7c1b2e0344e065064bc5cfc3 to your computer and use it in GitHub Desktop.
Convert PDF page to image using image magick
# create magick shell command to convert the first page of PDF file into image format
# Usage: convert_magic.sh <PDF file> <image format> <output name>
# convert_magic.sh input.pdf png output.png
# check if the PDF file exists
if [ ! -f $1 ]; then
echo "File $1 does not exist"
exit 1
fi
# check if the image format is specified
if [ -z $2 ]; then
echo "Image format is not specified"
exit 1
fi
# check if the image format is supported
if [ $2 != "png" ] && [ $2 != "jpg" ] && [ $2 != "gif" ]; then
echo "Image format $2 is not supported"
exit 1
fi
# check if desired output not specified
if [ -z $3 ]; then
echo "Output name not specified"
exit 1
fi
# convert the first page of PDF file into image format
magick convert -density 300 -quality 100 $1\[0\] $2:$3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment