Ghost Script - extract a single page from a pdf and convert it to a jpg

19,112

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.

Share:
19,112

Related videos on Youtube

Jason
Author by

Jason

I'm a web developer who works in PHP/MySQL/SQLite and Server side Javascript: http://bondijs.org

Updated on April 28, 2020

Comments

  • Jason
    Jason almost 4 years

    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.

  • curot
    curot about 11 years
    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.
  • mike
    mike about 5 years
    Yes! Removing the ImageMagick layer from my application and just calling Ghostscript directly created a very significant performance improvement.
  • Chris
    Chris almost 3 years
    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)

Related