banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Convert PDF Files To Graphic Files With Ease


by Paul Arnote (parnote)

PDF files have become as common as computing itself. Developed in the early 1990s, the Adobe Portable Document Format won the war for digital documents, against competing formats such as DjVu, Envoy, Common Ground Digital Paper, Farallon Replica, and even Adobe's own PostScript format.

Adobe made the PDF specification available free of charge in 1993, but it remained a proprietary format controlled by Adobe until its official release as an open standard on July 1, 1998. Adobe still holds a patent on the PDF format, but granted royalty free rights for all patents owned by Adobe that are necessary to make, sell, use and distribute PDF capable implementations.1

Virtually anyone who has used a computer has made use of PDF files. They really are THAT common. Many, many government forms are distributed as PDF files. But because Adobe still uses some proprietary technologies that are not standardized, users can face some compatibility problems when viewing PDF files utilizing these proprietary technologies on non-Adobe software. Even though Adobe publishes the specifications for these proprietary technologies on their website, there is no standardization of them or their use.

If you work a lot with PDF files (as we do here with The PCLinuxOS Magazine), you may find times when it's more practical to represent a PDF page (or pages) as a graphic file (or files). In fact, I do this every month with the magazine cover. I/we design the magazine cover in Scribus, and export it as a PDF file. That cover is reproduced every month as a graphic file on the magazine's website, as well as a few other places (our press releases, Google+, the PCLinuxOS forum, etc.).

As such, I'm always looking for a way to streamline the process, which is the same every single month. Scribus contains a feature that allows you to export the current page as a graphic file, but that feature has been broken in some of the latest Scribus releases. I used this feature religiously, every month, until it came up broken. Using my "backup plan," I can open a PDF file in GIMP and export selected page(s) as graphic files. I used this method to create the graphic file of the PDF cover as recently as the December 2016 issue. But even that process becomes arduous and a multi-step task. Now, I've found a better method that's way, way easier.


And Now Batting Cleanup ... ImageMagick

I never cease to be amazed by the abilities of the ImageMagick tools. ImageMagick isn't just a tool -- it's a veritable tool store of graphic utilities. There's little that the ImageMagick suite of command line tools cannot do, at least when it comes to graphic files.

Here's the command you need to use (from a command line in a terminal session), in its most basic form:

convert infile.pdf outfile.jpg

Beware that if you attempt this on a multipage PDF file, you'll get a separate graphic file for each page in the PDF file. So, I wouldn't try this on a PDF copy of War & Peace, if I were you. You'll end up with a whole slew of files named outfile-0.jpg, outfile-1.jpg, outfile-2.jpg, etc.

You don't have to use just JPG files, either. The "convert" command will also accept any other graphic file extension you want to use, such as "png," or even "bmp" (but does anyone even use that latter uncompressed graphic file any more?). The "convert" command will understand how to write out whatever graphic file format you specify, and adjust its output accordingly.

We can change this default activity by slightly modifying the command, as follows:

convert infile.pdf[0] outfile.jpg

Convert will treat the above command as telling it you want to create "page 1" of the specified PDF file as a JPG file. Since the convert command uses zero based page counting, page 1 of the PDF will be seen as page 0, page 2 as page 1, page 3 as page 2, etc.

In a similar manner, we can also specify a range of pages, like this:

convert infile.pdf[12-15] outfile.jpg

This command will take pages 13, 14, 15 and 16 of the PDF file, and create the JPG files as outfile-12.jpg, outfile-13.jpg, outfile-14.jpg and outfile-15.jpg. When specifying a range of pages from a PDF to convert into graphic files, don't forget to subtract one from the PDF page numbers to get the actual pages you are wanting to convert.

Of course, we can also use any of the other ImageMagick directives that the convert command understands. For example:

convert -resize 498 -bordercolor black -border 1x1 infile.pdf[0] outfile.jpg

This command will create a 500 pixel image, with a 1 pixel black border from the specified PDF file, and produce it as the specified output file. Don't despair. I didn't make a mistake by specifying an image width of 498 pixels. When the one pixel black border is added to both sides of the resulting image, the image will total 500 pixels in width.


Making It All "Automagic"

Since I use Xfce as my primary desktop environment, I've automated the process a bit more. First, I created a custom bash file, which I call mag-resize.sh. The name isn't important, just so long as I know what it does. I could have called it bettyboop.sh, had I wanted to. Here it is:



Then, to make it so that I never have to drop to a command line prompt to use my bash script, I created a Thunar custom action. So now, all I have to do is right click on the PDF file in Thunar, select the custom action from the context menu, and sit back. All of the "work" will be performed automatically, and all the size and format iterations of the magazine cover I need will magically appear in the same directory as the PDF file.



The above is the sample output from selecting the custom action in Thunar, which runs the bash script, producing the 452 pixel image above. The script produces a "full size" 792 x 612 pixel, high quality PNG file, as well as JPG versions 792, 600, 452, 256 and 170 pixels wide, complete with a one pixel black border. It's final act is to produce a cropped image of the cover that is used as the banner graphic each month on the magazine's website for the magazine's HTML edition.


Summary

The more you explore ImageMagick and its slew of command line tools, the more you will discover that it can do to make life easier for you. Until just recently, I wasn't aware that the convert command could read PDF files. But now that I know it can, it has unleashed some serious power, taking a previously time consuming task and making it so simple that my three year old son Ryan could perform the tasks.



1. Source: https://en.wikipedia.org/wiki/Portable_Document_Format


Previous Page              Top              Next Page