PHP ImageMagick - readimage not working for certain pdfs

2.7k views Asked by At

I`m currently trying to run the readimage() function on PDFs.

Here is the current code:

$image = new Imagick();
$image->readImage($file_to_read);
// etc...

Thing is, most PDFs work fine with this code. But for certain PDFs, PHP just hangs at the function call, throwing no exceptions nor causing a fatal error. Does anybody have an idea as to why or a workaround?

Thanks.

edit: I just ran $ gs -dNOPAUSE -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg 8Jun2015.pdf on the non-working pdf and it converted the image to jpg correctly. But readimage() still doesn't work on the same file.

1

There are 1 answers

0
Arnold Lam On BEST ANSWER

Turns out I have to specify the file path. The following code works.

putenv("PATH=/usr/local/bin:/usr/bin:/bin");
$im = new Imagick();
$im->readImage($file_to_read);

If it still doesn't work, you can "clean" the PDF using gs. Something like this:

$sh= shell_exec('/usr/local/bin/gs \
    -o /repaired_pdf/' .  $new_file_name . '\
    -sDEVICE=pdfwrite \
    -dPDFSETTINGS=/prepress \
    ' . $file_to_read);