Convert (Split) PDF Files into Images with ImageMagick and GhostScript

Split PDF

ImageMagick is an excellent open source set of software tools that helps with converting, editing, displaying and composing image files. Almost all programming languages have extensions or libraries to interact with the ImageMagick API, although you could also use it via command line.

Ghostscript is a set of tools that can interpret PostScript page description language and PDF files too, to render or rasterize them.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Using these tools, we can actually split apart a Portable Document Format (PDF) file into different images, 1 image per page.

Installation

On Mac, using homebrew its easy to install –

$ brew install imagemagick
$ brew install ghostscript

On Ubuntu, use APT –

$ sudo apt-get install imagemagick
$ sudo apt-get install ghostscript

Conversion

Using the convert tool which helps in conversion between various image formats as well as resize, crop, blur, etc. images, here’s how you’d convert a pdf file to an image –

$ convert file.pdf image.png

Note: If you try the command without installing ghostscript then you may get an error like this (as ImageMagick calls Ghostscript to do the work):

$ convert file.pdf image.png
convert: Postscript delegate failed `file.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/678.
convert: no images defined `image.png' @ error/convert.c/ConvertImageCommand/3078.

Specifying Page Number

As you’d have noticed, the command converts all pages of pdf to individual images with names like image-[\d].png. What if we want to convert the first or lets say the third page ? Thats simple! Just append [page_number] to the pdf file name:

$ convert file.pdf[0] image.png # convert the first page
$ convert file.pdf[2] image.png # convert the third page

Specifying Page Range

If you’d like to transform a range of pdf pages to images then the command slightly changes to something like this:

$ convert file.pdf[5-10] images/image.png
$ ls images/
image-10.png	image-6.png	image-8.png
image-5.png 	image-7.png	image-9.png

Converting multiple pages or ranges are also possible like this:

$ convert file.pdf[5-10,15-22,35] images/image.png
$ ls images/
image-10.png	image-17.png	image-20.png	image-35.png	image-7.png
image-15.png	image-18.png	image-21.png	image-5.png 	image-8.png
image-16.png	image-19.png	image-22.png	image-6.png 	image-9.png

Hope this quick tip helps you with extracting a pdf into its respective pages as images.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

3 thoughts on “Convert (Split) PDF Files into Images with ImageMagick and GhostScript”

Leave a Reply

Your email address will not be published. Required fields are marked *