Skip to content

Instantly share code, notes, and snippets.

@yura
Created November 10, 2010 15:18
Show Gist options
  • Save yura/670974 to your computer and use it in GitHub Desktop.
Save yura/670974 to your computer and use it in GitHub Desktop.
script to PDF to JPG using pdftk and imagemagick
#!/bin/bash
# Script to convert PDF file to JPG images
#
# Dependencies:
# * pdftk
# * imagemagick
PDF=$1
echo "Processing $PDF"
DIR=`basename "$1" .pdf`
mkdir "$DIR"
echo ' Splitting PDF file to pages...'
pdftk "$PDF" burst output "$DIR"/%04d.pdf
pdftk "$PDF" dump_data output "$DIR"/metadata.txt
echo ' Converting pages to JPEG files...'
for i in "$DIR"/*.pdf; do
convert -colorspace RGB -interlace none -density 300x300 -quality 100 "$i" "$DIR"/`basename "$i" .pdf`.jpg
done
echo 'All done'
@jedwards1211
Copy link

Thanks a lot for this!

@agurod42
Copy link

agurod42 commented Apr 3, 2017

Thanks a lot for your work! I've ported this script to Windows, here you can find my gist https://gist.github.com/agurz/7c72653bf1923804f26b8c0211de4669

@madmas
Copy link

madmas commented Oct 17, 2017

Hin: you need to have ghostscript installed for this as imagemagick relies on it. If it's missing you might see an error like

convert: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r300x300'  '-sOutputFile=/var/folders/w_/bn46lxks695dg9yvvrgx_xnm0000gn/T/magick-23598b4Fr54IpZ4vE%d' '-f/var/folders/w_/bn46lxks695dg9yvvrgx_xnm0000gn/T/magick-23598hwvO5gF963Ot' '-f/var/folders/w_/bn46lxks695dg9yvvrgx_xnm0000gn/T/magick-23598JknpjTH6yPWj'' (1) @ error/pdf.c/InvokePDFDelegate/291.
convert: no images defined `0002.jpg' @ error/convert.c/ConvertImageCommand/3275. ```

Thanks a lot for the script!

@Strae
Copy link

Strae commented Oct 31, 2017

thanks man, really usefull!

@rolandog
Copy link

Hin: you need to have ghostscript installed for this as imagemagick relies on it. If it's missing you might see an error like

convert: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r300x300'  '-sOutputFile=/var/folders/w_/bn46lxks695dg9yvvrgx_xnm0000gn/T/magick-23598b4Fr54IpZ4vE%d' '-f/var/folders/w_/bn46lxks695dg9yvvrgx_xnm0000gn/T/magick-23598hwvO5gF963Ot' '-f/var/folders/w_/bn46lxks695dg9yvvrgx_xnm0000gn/T/magick-23598JknpjTH6yPWj'' (1) @ error/pdf.c/InvokePDFDelegate/291.
convert: no images defined `0002.jpg' @ error/convert.c/ConvertImageCommand/3275. ```

Thanks a lot for the script!

Thanks for clearing that up!

@nssy
Copy link

nssy commented Sep 30, 2020

Very Nice. Thanks

@dvaupel
Copy link

dvaupel commented Jul 7, 2022

Simpler solution is to just use pdftoppm:

pdftoppm -jpeg {pdf file} {output folder}

Install with sudo apt install poppler-utils.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment