Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@jedwards1211 jedwards1211 commented Nov 8, 2015

Thanks a lot for this!

@rey809

This comment has been minimized.

Copy link

@rey809 rey809 commented Mar 30, 2017

How this it works can you help me? :(

@agurodriguez

This comment has been minimized.

Copy link

@agurodriguez agurodriguez 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

This comment has been minimized.

Copy link

@madmas 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

This comment has been minimized.

Copy link

@Strae Strae commented Oct 31, 2017

thanks man, really usefull!

@rolandog

This comment has been minimized.

Copy link

@rolandog rolandog commented Mar 15, 2019

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

This comment has been minimized.

Copy link

@nssy nssy commented Sep 30, 2020

Very Nice. Thanks

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