Convert a multi-page PDF file into multiple image files

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT
Published:
Updated:
This article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly simple — a single line of code!
In three previous articles here at Experts Exchange, I discussed the excellent (free!) GraphicsMagick package. The first article, Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method, explains how to download and install GraphicsMagick, which is required for the solution presented in this new, fourth article utilizing GraphicsMagick. So if you don't already have GraphicsMagick installed, please read that article.

The second article in the series, Create a PDF file with Contact Sheets (montage of thumbnails) for all JPG files in a folder and each of its subfolders using an automated, batch method, provides a solution to the question of creating a multi-page PDF file from many JPG files, while the third article, Create an image (BMP, GIF, JPG, PNG, TIF, etc.) from a multi-page PDF, provides a solution for creating a JPG file from a multi-page PDF file. This new article addresses a similar, yet different, requirement from a recent question here at EE, namely, conversion of a multi-page PDF into multiple image files — one image file for each page of the PDF.

As with the other solutions, this one is amazingly simple — a single command line call to GraphicsMagick! Here's an example of what the solution looks like:
 
gm.exe convert input.pdf +adjoin output%02d.jpg

Open in new window


The key to the solution is the +adjoin option, with the "+" being crucial. This tells GraphicsMagick to convert the input file into multiple, numbered output files. Because of this, the output file name must include a printf style formatting specification for the numeric part of the output file name (it may have any string for the leading part of the file name). There are many options in specifying the printf formats, and a complete explanation of printf is beyond the scope of this article. However, for the sake of completeness, let's take a look at the options used in the line of code above:

%02d

The "%" is always the beginning character of a printf format. The "d" means that the generated value is a decimal integer and the "02" means that it should be two digits in length, padded with a "0" on the left when necessary. With this explanation, the printf format used in the code sample above will result in these files:

output00.jpg
output01.jpg
output02.jpg

Likewise, if the line of code above ended in...

image_%03d.jpg

...the resulting files would be:

image_000.jpg
image_001.jpg
image_002.jpg

If you'd like to learn more about printf, a web search will provide much to study.

Here's another example, shown by a screenshot of the results in a Command Prompt window before and after running the gm.exe command on the six-page PDF file attached to my previous GraphicsMagick article:

GM-command-prompt-results.jpg
The screenshot shows that there was a single PDF file in the folder before issuing the command (input.pdf) and six JPG files after it ran.

There are many more options in the convert sub-command, such as those mentioned in my other three articles, as well as in the documentation at the GraphicsMagick website. If you need more control over the output images, I recommend experimenting with the numerous options.

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
6
9,941 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT

Comments (5)

Informative article
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Methew,
Thanks for the compliment — I appreciate it. Regards, Joe
Miguel MonteiroPresentations Master Designer

Commented:
Enlighten me please, although free, what's the use of an external solution for doing something which is already part of the original software - Acrobat ? "Export / (choose which type)" or "Save as/Save as type (choose which)"?
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Miguel,

> although free

A crucial factor for some folks. Acrobat is expensive software — not everyone can afford it. You'll notice in the original question here at EE that inspired this article, the asker said, "If anyone has another free option, let me know."

> what's the use of an external solution doing something which is already part of the original software - Acrobat

Acrobat is also "external" in the sense that it is not part of Windows. For many users, Acrobat is not part of their "original software".

> "Export / (choose which type)" or "Save as/Save as type (choose which)"?

For one file (or a small number of files), this is fine. But many users have hundreds, even thousands, of files on which to operate. In that case, the manual Export or Save As is not the ticket. If you have Acrobat, the Action Wizard will work in some cases, but sometimes the better solution, imo, is a command line tool like gm.exe that can be called from a batch file, program, or script. I have written many programs that call gm.exe to operate on all files in a folder and, often, to recurse into subfolders to an unlimited depth, processing all files along the way. Also, you have a lot more control over the output files, with a fantastic set of options. Acrobat is a fine product (I have both Standard and Professional), but in many situations, I prefer writing a program that calls GraphicsMagick, as well as other software that supports a command line interface, such as Nuance's Power PDF — my EE article, Batch Conversion of PDF, TIFF, and Other Image Formats via Command Line Interface to PDF, PDF Searchable, and TIFF, discusses a good example of that. 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.