28

I'm using ubuntu 10.10 and I have ghost script installed. What I need to do is to extract one page from my PDF and then convert that page to a jpg. The aim is to create a PDF previewer....

Here's some settings I found that apply to a windows version of ghostscript to convert the entire thing into a jpg. It doesn't let me isolate one page and that's really what i need.

    -dBATCH ^
    -dNOPAUSE ^
    -dSAFER ^
    -sDEVICE=jpeg ^
    -dJPEGQ=30 ^
    -r72x72 ^
    -sOutputFile=c:/path/to/jpeg-dir/pdffile-%03d.jpeg ^
    /path/to/pdffile.pdf

I then need to write this into my PHP library so that I can just run a function like $img_src = pdf::preview('test.pdf', $page=1);

Does anyone have any idea about this?

Thanks

ESP Ghostscript 815.02 (2006-04-19) Copyright (C) 2004 artofcode LLC, Benicia, CA. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details.

Jason
  • 15,064
  • 15
  • 65
  • 105

1 Answers1

48

Are you saying you want to extract a single page from the PDF? Let's say you want to extract page 12. You can do that with Ghostscript using the following options:

-dFirstPage=12 -dLastPage=12

Just add those two options to the options you have above, changing the "12" to the page you want to extract.

If you were running it from terminal, it would look like this:

gs \
 -sDEVICE=jpeg \
 -o %03d.jpeg \
 -dFirstPage=12 \
 -dLastPage=12 \
 -dJPEGQ=30 \
 -r72x72 \
  file.pdf

I really don't recommend using Imagemagick for converting a PDF to a JPEG; it uses Ghostscript to do so, anyway, and is slower than using Ghostscript directly. I have done some experimentation and found that you can get higher-quality images by using Ghostscript to output a high-quality JPEG and then using Imagemagick's mogrify command to resize and compress the image, though that may be due to my limited knowledge of Ghostscript rather than limitations of it. If you're just creating 72 x 72 thumbnails, it probably isn't important.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
linux4me
  • 855
  • 5
  • 11
  • 1
    In my experience this is definitely true, ImageMagick is calling GS under the covers anyway except I think it concatenates and then reads the pages into memory which is really slow. Ghostscript is faster, uses less memory, and is better quality than using ImageMagick for this task. – Ibrahim Apr 09 '13 at 07:09
  • Yes! Removing the ImageMagick layer from my application and just calling Ghostscript directly created a very significant performance improvement. – mike Apr 09 '19 at 22:25
  • `gs` pages are base-1 rather than base-0 (some useful info for this question, since `gs` error message with an incorrect page range is generic) – Chris Jul 20 '21 at 16:22