Link to home
Start Free TrialLog in
Avatar of micamedia
micamedia

asked on

Converting PDF to Image via PHP

I need to take a PDF and extract all pages of the document as seperate images via php. How do i go about doing this? (and if it's possible to descrease the resolution while doing so .. that would be great. The source files are 300 dpi and I really only need them 72 pdi for showing them on the screen.)

Dan
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of micamedia
micamedia

ASKER

do you have any links with information on how to install these and more detailed information on how to use it?

Hi micamedia,

Any google search could dig it up (http://www.imagemagick.org).  I second shmert that convert's probably the best choice for the job.

Cheers!
but i'm not clear on if this runs via php or via the shell. and if it's via the shell then i am just executing shell commands via phps exec command right?

The question that comes up is php executes via the user "nobody" will it have the correct access to read and write and access the imagemagik program?

Of course i will research it myself later.. but now i have to run.

Dan
also am i going to need root access to install imagemagic? or can i just use the DL command to link it in PHP..

I'll check it out soon. Just looked at the web site seems that it does interface directly with php, but i'm not sure if all the commands are avaibalbe via php.

Still shouldn't be to hard to run a shell command if the nobody user can do so.
>but i'm not clear on if this runs via php or via the shell. and if it's via the shell then i am just executing shell commands via phps exec command right?

Yes.

>The question that comes up is php executes via the user "nobody" will it have the correct access to read and write and access the imagemagik program?

You'd have to chmod 0777 the directory where you intend to have images written

>also am i going to need root access to install imagemagic? or can i just use the DL command to link it in PHP..

No, you can install it to your directory.  It works fine this way too.

BTW, shmert seems to like using imagemagick (well I do too), but I think for your other question GD would do just fine and it's easy to use php API to do it.
can GD convert PDF to jpg/gif?
no, GD can't read PDFs.  ImageMagick needs the ghostscript lib to read/write PDFs.

You'll want to install both of these libs, and make sure ImageMagick can locate the ghostscript libs when called from the shell (without PHP, just for testing).  Just run the command above,

convert -scale 120x80 myfile.pdf converted.png

Once this works, you'll do the same thing from php, using exec, but specify the full path to the imagemagick binary when you call it from PHP:

exec('/usr/local/bin/convert -scale 120x80 myfile.pdf converted.png');

As far as permissions, 'nobody' only needs permission to execute the 'convert' command (which is by default executable for anyone), and the permission to write to the target directory.

What platform are you on?
freebsd 4.8
>but I think for your other question GD would do just fine and it's easy to use php API to do it.

I meant that for your other question "image zoom and pan" or something like that GD is just fine and should be better, without the hassle of installing imagemagick.  For imagemagick PDF on freebsd just search for their ports.  They have to be there.