3

I am trying to convert a multi-page PDF file into a bunch of JPEGs, one for each page in the PDF. I have spent hours and hours looking up how to do this, and eventually I discovered that I need Ghostscript installed. So I did that (from this website: http://downloads.ghostscript.com/public/ And I used the most recent link "ghostscript-9.05.tar.gz" from Feb 8, 2012).

However, even with this installed/downloaded, I am still unable to do what I want. Should I have this saved somewhere special, like in the same folder as ImageMagick?

What I have figured out so far is this:

  • In Command Prompt I change the working directory to the ImageMagick folder, where that is saved.

  • I then type

    convert "<full file path to pdf>" "<full file path to jpg>"
    

This is followed by a giant blob of error. It begins with:

    Unrecoverable error: rangecheck in.setuserparams
    Operand stack:

Followed by a blurb of unreadable numbers and caps. It ends with:

    While reading gs_lev2.ps:
    %%[ Error: invalidaccess; OffendingCommand: put ]%%

Needless to say, after hours and hours of deliberation, I don't think I am any closer to doing the seemingly simple task of converting this PDF into a JPG.

What I would like are some step by step instructions on how to make this work. Don't leave out anything, no matter how "obvious" it might seem (especially anything involving ghostscript). This has been troubling me and my supervisor for months now.

For further clarification, we are on a Windows XP operating system. The eventual intention is to call these command lines in R, the statistical language, and run it in a script. In addition, I have been able to successfully convert JPGs to PNG format and vice versa, but PDF just is not working.

Help!!!

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Stephen
  • 95
  • 1
  • 1
  • 8
  • (Removed R tag; you cannot even do it at the command line yet.) Do you even have a `convert` executable on your path? I looked through the GS documentation and found quite different instructions on how to use it for conversions. – IRTFM Jun 12 '12 at 19:15
  • Does it work with single-page PDFs? If you install pdftk, you can type `pdftk input.pdf burst` to create `input_0001.pdf`, `input_0002.pdf`, and so forth. You could then try calling `convert input_0001.pdf input_0001.jpg` for every page. Does that still give the same error message? – Esteis Jun 12 '12 at 19:16
  • Is this answer on point? http://stackoverflow.com/questions/4481573/how-to-override-windows-convert-command-by-imagemagicks-convert-command – IRTFM Jun 12 '12 at 19:31
  • DWin, I do have a convert executable. It's in my ImageMagick folder. Using it I could convert between JPG/PNG. But I can't do any conversions at all from PDF (Not even a single page. But I CAN convert from JPG to PDF, just not from PDF)). From the other places that I've read, I feel I should use GhostScript, but I don't know how to implement it. – Stephen Jun 12 '12 at 19:49
  • I install GS first then Imagemagick rebooting the PC before testing and it just works. Can you do other things with Imagemagick to prove that is working? – Bonzo Jun 12 '12 at 19:52
  • I'll try reinstalling and restarting in that order, Bonzo. But yes, I just tested the IM convert function again, and it worked perfectly for JPG to PNG. It doesn't handle PDF to JPG, though. :s – Stephen Jun 12 '12 at 19:54
  • Checkout [Using Ghostscript to convert multi-page PDF into single JPG](http://superuser.com/questions/168444/using-ghostscript-to-convert-multi-page-pdf-into-single-jpg). – dma_k Nov 15 '12 at 11:37

2 Answers2

15

You don't need ImageMagick for this, Ghostscript can do it all alone. (If you used ImageMagick, it couldn't do that conversion itself, it HAS to use Ghostscript as its 'delegate'.)

Try this for directly using Ghostscript:

 c:\path\to\gswin32c.exe ^
   -o page_%03d.jpg ^
   -sDEVICE=jpeg ^
    d:/path/to/input.pdf

This will create a new JPEG for each page, and the filenames will increment as page_001.jpg, page_002.jpg,...

Note, this will also create JPEGs which use all the default settings of the jpeg device (one of the most important ones will be that the resolution will be 72dpi).

If you need higher (or lower resolution) for your images, you can add other options:

 gswin32c.exe ^
   -o page_%03d.jpg ^
   -sDEVICE=jpeg ^
   -r300 ^
   -dJPEGQ=100 ^
    d:/path/to/input.pdf

-r300 sets the resolution to 300dpi and -dJPEGQ=100 sets the highest JPEG quality level (Ghostscript's default is 75).

Also note, please: JPEG is not well suited to represent shapes with sharp edges and high contrast in good quality (such as you typically see in black-on-white text pages with small characters).

The (lossy) JPEG compression method is optimized for continuous-tone pictures + photos, and not for line graphics. Therefore it is sub-optimal for such PostScript or PDF input pages which mainly contain text. Here, the lossy compression of the JPEG format will result in poorer quality output even if the input is excellent. See also the JPEG FAQ for more details on this topic.

You may get better image output by choosing PNG as the output format (PNG uses a lossless compression):

 gswin32c.exe ^
   -o page_%03d.png ^
   -sDEVICE=png16m ^
   -r150 ^
    d:/path/to/input.pdf

The png16m device produces 24bit RGB color. You could swap this for pnggray (for pure grayscale output), png256 (for 8-bit color), png16 (4-bit color), pngmono (black and white only) or pngmonod (alternative black-and-white module).

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Thanks for the response! When I try typing in what you gave me to the command prompt, it says "gswin32.exe-o is not recognized as an internal or external command, operable, or batch file." Should I not be typing in the command prompt? Or should I have a different directory? – Stephen Jun 12 '12 at 20:29
  • You seem to be missing a space: `gswin32.exe -o ...`, not `.exe-o`. – Esteis Jun 12 '12 at 20:41
  • Nope, it still says "gswin23c.exe is not recognized as an internal or external command, operable program, or batch file." – Stephen Jun 12 '12 at 20:46
  • 1
    @Stephan Robert Kuban: No, you didn't type what I gave you. My example has a blank following the `.exe`, before the `-o`... -- Also, the `^` at the end of each line means you can copy all the lines (including the `^`) as one block into the command prompt and execute it. It's the *line continuation* sign for the 'DOS prompt' and for batch files. Alternatively, you can type it all in one line (but then please skip all the `^` characters!). – Kurt Pfeifle Jun 12 '12 at 20:49
  • 1
    Of course I assume you know how to run commands from the command prompt and to set the PATH environment variable correctly (you have the directory in which `gswin32c.exe` is installed in your PATH). Otherwise, just use the full path like this: `e:\path\to\gswin32c.exe`... – Kurt Pfeifle Jun 12 '12 at 20:50
  • Ah, I think I see the issue. I don't have gswin32c.exe downloaded. The ghostscript file which I downloaded is: "ghostscript-9.05.tar". Where can I obtain gswin32c.exe ? (Sorry about this! I'm rather new to command prompt!) – Stephen Jun 12 '12 at 20:56
  • OMG! The \*.tar file you downloaded does contain the *program source code* of Ghostscript, which would mean you'd have to compile it yourself into an *executable program*. You're better off downloading the installer file called *gs905w32.exe* from http://downloads.ghostscript.com/public/ and run this on your Windows... – Kurt Pfeifle Jun 12 '12 at 21:02
  • I've run it using gs905w32.exe, and it redirects me to an installation menu. Since I'm on a work computer, I don't have the administrative rights to install anything, I can only download. Is there a way around this? Thank you for helping me out, this is very frustrating for me. – Stephen Jun 12 '12 at 21:11
-3

There are numerous SaaS services that will do this for you too. HyPDF and Blitline come to mind.