Google Answers Logo
View Question
 
Q: Create Image from PDF using PHP ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Create Image from PDF using PHP
Category: Computers > Programming
Asked by: g8z-ga
List Price: $10.00
Posted: 29 Dec 2002 00:54 PST
Expires: 28 Jan 2003 00:54 PST
Question ID: 134543
Hello, 

I need to create an image FROM a PDF file using PHP or Perl (or other
common server-side scripting language).

Consider this example: I have a one-page PDF file, and I have a user
who (for one reason or another) cannot install Adobe Acrobat Reader.
Thus, I want the user to be able to view the PDF as an image (gif,
jpg, or png), but I want to be able to do this programmatically -
i.e., I don't want to have to take a screen shot and upload the image
myself.

You may assume that I have access to many of the most common PHP
multimedia libraries, like PDFLib, ImageMagick, and GD, and that PHP
or Perl modules can be installed if needed. Although not currently
installed on the server, I could probably also use Java/Servlet
technology if there is no way to do this in PHP or Perl.

How would I accomplish this?

Thanks,
Darren
Answer  
Subject: Re: Create Image from PDF using PHP
Answered By: samrolken-ga on 29 Dec 2002 07:04 PST
Rated:5 out of 5 stars
 
Darren,

To do what you want to do, you need GhostScript and ImageMagick
installed. Your script (in PHP, Perl, or whichever) can then call
ImageMagick's "convert" program, and make the conversion.

convert sample.pdf sample.jpeg

-or-

convert sample.pdf sample.png

So, to make the conversion in PHP, just do: 

exec("convert sample.pdf sample.jpeg")

You might also find this helpful:
http://www.samurajdata.se/~cj/ps.html

More information:

http://www.php.net/manual/en/function.exec.php
http://www.imagemagick.org/www/formats.html

Search Stragegy:

"php pdf" on google.com

--
samrolken-ga

Request for Answer Clarification by g8z-ga on 29 Dec 2002 08:55 PST
hi,
I did as you said and received an error, "Portable Document delegate
failed". I cut and pasted the commands from the Unix command line
below. The first command verifies that ghostscript is installed, the
second command verifies that ImageMagick is installed, the 'ls' shows
that I have a pdf present, and the last command is the conversion
attempt. What am I doing wrong?

[windsor@borg alphatest]$ ghostscript
GNU Ghostscript 6.52 (2001-10-20)
Copyright (C) 2001 artofcode LLC, Benicia, CA.  All rights reserved.
This software comes with NO WARRANTY: see the file COPYING for
details.
GNU Ghostscript 6.52: Cannot open X display `(null)'.
**** Unable to open the initial device, quitting.
[windsor@borg alphatest]$  
[windsor@borg alphatest]$ identify logo:
 640x480+0+0 PseudoClass 256c 26kb GIF 0.0u 0:01
[windsor@borg alphatest]$ 
[windsor@borg alphatest]$ ls
test.pdf
[windsor@borg alphatest]$ convert test.pdf test.jpeg
convert: Portable Document delegate failed (test.pdf).
convert: Missing an image file name.

Clarification of Answer by samrolken-ga on 29 Dec 2002 15:58 PST
Please tell me which operating system you are using so that I may
provide instructions for installing or compiling a ghostscript-aware
version of ImageMagick.

--samrolken-ga

Request for Answer Clarification by g8z-ga on 29 Dec 2002 16:34 PST
hi,

The system is Linux. However, I'm not sure if recompiling is an option
available to me. The website that I'm trying to do this on is remotely
hosted, although I do have telnet access. In other words, I don't have
root privileges.

Is there some way to do the conversion by calling a ghostscript
function first, and then calling ImageMagick on the result, or by some
other method altogether? If not, then please provide the recompilation
instructions and I'll see if the hosting company will do it for me or
if it's possible via telnet.

Thanks,
Darren

Clarification of Answer by samrolken-ga on 29 Dec 2002 16:48 PST
Can you try executing this:

gs -sDEVICE=jpeg -sOutputFile=test.jpg test.pdf

-- samrolken-ga

Clarification of Answer by samrolken-ga on 03 Jan 2003 04:35 PST
As a little follow-up to your question, I wanted to let you know that
I came across the source code to a site that does the exact thing that
you want to do. The guy who runs http://www.samurajdata.se/~cj/ps.html
was kind enough to send me the GPL'ed source code to his website
(written in PHP) that does the exact same thing that you need to do. I
have put it up at http://samk.org/files/psview-2.0.tar.gz. I hope this
helps, and I wish you luck.

-- samrolken-ga

Request for Answer Clarification by g8z-ga on 04 Jan 2003 17:50 PST
hi,

Thanks for the additional clarification. It looks like your answer
does exactly what I need.

I tried the link that you provided and it seems to be invalid... could
you check that URL one more time?

Thanks for an excellent answer.

Clarification of Answer by samrolken-ga on 04 Jan 2003 18:53 PST
Oops! Sorry about that. The correct URL is
http://samk.org/files/psview-0.2.tar.gz .

--
samrolken-ga
g8z-ga rated this answer:5 out of 5 stars
Great followups. Thanks for the links.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy