Convert PDFs to JPGs from the Commandline

ImageMagick's convert commandWhen I first switched to Linux, I also attempted to switch to Gimp—which is generally considered to be Linux’s Photoshop. My experience with Gimp, especially versions before 2.5, was not great, and I switched back to Photoshop pretty quickly.

One of my freelance clients publishes a newsletter every month, and I convert the PDF into JPG images to use on the site as a sort of low-fi online version. I discovered pretty quickly that Gimp’s conversion routines did a terrible job. Eventually, I found a solution using ImageMagick, which I had used previously in my bipolar gallery scripts.

Converting images is super easy with ImageMagick’s convert command, and as it turns out, convert can also handle PDF files.

Here’s the commandline I use:

convert -density 300 -size 4100 sourcefile.pdf -quality 100 outputfiles.jpg

Basically, this converts each page of the pdf into a 300ppi JPG with a maximum dimension of 4100 pixels and a JPG quality setting of 100% (or no compression). Each page is saved as an individual JPG named outputfilesXX.jpg (where XX represents the page number, starting at 00).

This gives me much higher-quality starting images than Gimp provided, which I can then resize as needed.

convert has a huge array of features beyond this simple example, and is worth a look if you need to do image manipulation from a script, or batch operations on images from the commandline.

3 Responses

  1. lisa says:

    Can’t you just open up the PDF in Photoshop and save it as a JPEG? Or export the PDF from Acrobat? You must be trying to do this the super techy way. :)

  2. matt says:

    Hehe. Sort of. I was trying to find a way to do it on Linux, without having to run Photoshop or Acrobat in a Virtual Machine. (I’m well past that now, running Windows in a VM daily, but I digress). Also, at the time, I think Photoshop would only import a single page of a PDF at a time, so I’d have to re-open the file and select each individual page to get it all converted. I don’t think that’s an issue anymore, but i haven’t used Photoshop to try it since discovering this trick.

    ImageMagick is available for Windows and Mac as well, but this is mainly a tip for Linux users. On the positive side, Linux PDF tools have also improved in the last few years, so there may be a better GUI solution as well.

    If you’re on Windows or Mac (or using recent versions of Wine on Linux), running Photoshop is a no-brainer, if you can afford it.

  3. lisa says:

    Ooh, that is fancy. It’s got to be handy knowing how to do those types of things the real nuts and bolts way.

    I like that there’s a program that runs software called Wine. :)