Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 759
Jason Avatar asked Apr 03 '11 05:04

Jason


1 Answers

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.

like image 87
linux4me Avatar answered Sep 21 '22 03:09

linux4me