Categories: LinuxWeb development

Convert PDF to Image(s) using ImageMagick

There is a quick and convenient way to convert PDF to one or more images. Command line tool ImageMagick does that (and a lot more). You can convert an entire PDF document to a single image, or, if you like, there is an option to output pages as a series of enumerated image files.

Note: It is advisable to use density, antialias and quality options to get the conversion quality that suits your needs. In following examples we are going to use -density 150, -quality 100 and -antialias options. ImageMagick is intuitive and it will guess the output format based on output filename extension.

Examples:

Convert an entire PDF to a single image:

convert -density 150 -antialias "input_file_name.pdf" -append -resize 1024x -quality 100 "output_file_name.png"

Key parameter here is -append which actually makes a difference if PDF is converted to a single image or to a series of images.

Convert a PDF document to a series of enumerated images:

convert -density 150 -antialias "input_file_name.pdf" -resize 1024x -quality 100 "output_file_name.png"

As a result of this command a series of image files named output_file_name-0.png, output_file_name-1.png, output_file_name-2.png …. etc, will be created in the working directory. If you have more then 10 pages, it would come in handy to have those enumerated file names with multiple digits, for the convenience of easy sorting. If you include a C-style integer format string, for example if you add %03d to the end of your output file name, you will get output_file_name-001.png, output_file_name-002.png, output_file_name-003.png, etc.

convert -density 150 -antialias "input_file_name.pdf" -resize 1024x -quality 100 "output_file_name-%03d.png"

Convert only specified pages to images:

convert "input_file_name.pdf[1]" "output_file_name.png"

This will actually convert page 2 of PDF to PNG, since numbering starts with 0.  To convert range of pages, from i to j, use this command:

convert "input_file_name.pdf[i-j]" "output_file_name.png"

Using with PHP

Since this is a command line utility, you can access it from PHP using some of the functions to execute external commands. For example, exec, system, passthru, etc.

View Comments

  • Hi Thomas, the third one should convert all the pages. Could you share more details of what exactly does not work for you?

Recent Posts

Sonata Admin: modify validation groups in an admin extension

SonataAdminBundle has a powerful extensions feature which allows you to add or change features of…

4 years ago

Grandfather’s advices

I used to spend a lot of time with my grandparents in my childhood. My…

6 years ago

World. World never stops changing.

If anything is certain in this world, it is that things always change. And there…

6 years ago

How to be successful

This is the most important advice on how to be successful! Read it carefully!

8 years ago

I wanted to change WordPress URL, you won’t believe what happened afterwards

WordPress has a nasty habit of storing absolute URLs in the database. That means that…

9 years ago

Let’s Encrypt – providing free automated SSL certificates

In August 2014 Google announced that their search engine will value HTTPS as a ranking…

9 years ago