Create an image (BMP, GIF, JPG, PNG, TIF, etc.) from a multi-page PDF

Published on
27,618 Points
1 Endorsement
Last Modified:
Joe Winograd
50+ years in computer industry •Everything from development to sales •CIO •Windows •Document Imaging •EE MVE 2015,2016,2018 •EE FELLOW 2017
In a previous article here at Experts Exchange, I presented a solution to the question of creating a multi-page PDF file containing contact sheets (aka a montage of thumbnails) for all JPG files in a folder. A recent question here at EE asked for a solution, in essence, to the reverse problem, that is, creating a JPG (or PNG) from a multi-page PDF file. In this case, the solution requires all pages of the PDF to be incorporated into a single image. At the question, I posted a relatively brief solution that uses the same GraphicsMagick software and approach as discussed in the article referred to above. Then I decided to write this new article with a more comprehensive explanation of the solution, including some interesting options.

The method presented here also requires the GraphicsMagick software. Another EE article explains how to download and install it. With GraphicsMagick installed, we're ready to proceed with the solution, which is amazingly simple — one line of code in a command prompt! Here it is:
gm.exe montage input.pdf output.jpg

Open in new window

Below is the resulting JPG from the command above using the six-page input.pdf file attached to this article:

As you can see, the default is to place the multiple pages of the PDF horizontally in the output image. If you prefer a different placement, use the -tile option, which is in the format of columns-by-rows. For example, here's the command to place them 3x2:
gm.exe montage -tile 3x2 input.pdf output.jpg

Open in new window

Below is the resulting JPG from the command above:

If you don't know how many pages are in the PDF, decide on the number of columns you want and make the number of rows very large in order to assure that a single image is created, that is, make sure the number of tiles specified by the -tile parameter is at least as large as the number of input images. For example, you could use 10,000 as the number of rows with 2 as the number of columns, as follows:
gm.exe montage -tile 2x10000 input.pdf output.jpg

Open in new window

With the sample PDF we've been using, the above command creates this JPG:

It is possible to have the output be many other file types, such as:
gm.exe montage input.pdf output.bmp
gm.exe montage input.pdf output.gif
gm.exe montage input.pdf output.png
gm.exe montage input.pdf output.tif

Open in new window

That is, simply specify the appropriate file extension on the name of the output file for the file format that you want. I don't know if all the GraphicsMagick supported formats will work in this particular case, but I have tested BMP, GIF, JPG, PNG, and TIF, all of which work perfectly.

You may, of course, use other options on the GraphicsMagick command line. For example, here's a call that creates a nicer looking version of the image by specifying the -frame option (surrounds each tile with an ornamental border), the -mattecolor option (the color of the frame), and the -shadow option (puts a shadow effect on each tile):
gm.exe montage -tile 2x10000 -frame 12x10 -mattecolor gray -shadow input.pdf output.jpg

Open in new window

With the sample PDF, the above command creates this image:

There are many more options in the montage sub-command — I recommend experimenting with them.

If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe


Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free