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

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
CERTIFIED GOLD 2020
Published:
Updated:
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:

output.jpg
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:

output-3x2.jpg
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:

output-2x10000.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:

output-pretty.jpg
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

input.pdf


 
3
5,938 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
CERTIFIED GOLD 2020

Comments (4)

Jerry LOperations Manager

Commented:
@Joe Winograd
Thank you for the well presented explanation. @Jazz Kaur referred me to this when I was asking (in EE) how to perform this transformation from multiple pages in PDF to a Single Image file.

This may not be necessary but are you aware of a similar solution but where you stack the PDF pages into one PDF File with a SINGLE PAGE? This might retain text (using native fonts) that is not distorted or aliased (I'm doing this for Math Instruction and Exercises and uploading to Moodle LMS). Then you can export or "save as" to a single image file without needing to use an Image Tool or Editor. I happen to need only one Column with Multiple Rows. But having [Row x Column] (or [Column x Row]) options would be handy. The single page size might be a problem if there are too many pages being pushed into only one PDF page - Not sure if this is even practical.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Jerry,

> Thank you for the well presented explanation.

You're welcome...and thanks back at you for endorsing the article...much appreciated!

> Are you aware of a similar solution but where you stack the PDF pages into one Single PDF file, and then you can export or "save as" to a single image file?

This solution can do that, too. Simply set the number of columns to 1 and change the output file type to PDF, such as:

gm.exe montage -tile 1x10000 input.pdf output.pdf

Open in new window

Using the input file presented in this article, the output PDF file from that command is attached. Note that the maximum tile size by default is 120x120. You can change that with the -geometry parameter, such as:

gm.exe montage -tile 1x10000 -geometry 600x600 input.pdf output.pdf

Open in new window

The PDF file from that command is also attached.

> I happen to need only one Column with Multiple Rows.

OK, that's what the above does.

> But having [Row x Column] options would be handy.

Indeed! Btw, the parameter to GraphicsMagick is Columns x Rows.

> then you can export or "save as" to a single image file

Three ways to achieve that:

(1) From the original PDF, simply change the output file type to JPG (or PNG or whatever image file type you want), such as:

gm.exe montage -tile 1x10000 input.pdf output.jpg
gm.exe montage -tile 1x10000 -geometry 600x600 input.pdf output.png

Open in new window

(2) From the single-page (output) PDF, convert it to to JPG (or PNG or whatever image file type you want), such as:

gm.exe convert output-600x600.pdf output.jpg

Open in new window

You may want to control the quality of the output JPG file with the -quality parameter, such as:

gm.exe convert -quality 100 output-600x600.pdf output.jpg

Open in new window

Another one of my EE articles discusses the -quality parameter:

Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method

(3) If you're OK with an output file of PNG, the Xpdf utility PDFtoPNG does a superb job. One of my five-minute EE video Micro Tutorials explains how/where to download the utilities:

Xpdf - Command Line Utilities for PDF Files

Another one discusses the PDFtoPNG tool specifically:

Xpdf - PDFtoPNG - Command Line Utility to Convert a Multi-page PDF File into Separate PNG Files

Although it can convert a multi-page PDF file, it works fine, of course, on a one-page PDF file, such as the output file that you create via GraphicsMagick using the steps above. Attached is the PNG created by PDFtoPNG from the output-600x600.pdf file and setting the resolution to 600 on the PDFtoPNG run.

Regards, Joe
output.pdf
output-600x600.pdf
output-600x600-Xpdf-PDFtoPNG-res600.png
Jerry LOperations Manager

Commented:
Yes, that's it, very nice! Thank you.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
You're welcome, Jerry. And thanks for endorsing one of my other GraphicsMagick articles (Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method). Regards, Joe

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community