1
votes

I'm trying to convert a pdf file into png. It works on command line, but it won't work via php code. I tried this code example from the php.net:

<?php 
  $imagick = new Imagick(); 
  $imagick->readImage('myfile.pdf'); 
  $imagick->writeImages('converted.jpg', false); 
?>   

When I use a relative path, it gives the following error:

Fatal error: Uncaught exception 'ImagickException' with message 'UnableToOpenBlob 'myfile.pdf': No such file or directory @ error/blob.c/OpenBlob/2702' in C:\xampp\htdocs\portalgoogle\views\ticket.php:4 Stack trace: #0 C:\xampp\htdocs\portalgoogle\views\ticket.php(4): Imagick->readimage('etiqueta.pdf') #1 {main} thrown in C:\xampp\htdocs\portalgoogle\views\ticket.php on line 4

When I use the fullpath, it just gives a fatal error on the readImage line. Could someone show me what I need to do or suggest an easier way to convert pdf to png? I tried pdf.js, but it didn't work on Chrome.

1
Is myfile.pdf in the same directory as your PHP file? What is your directory structure?BenM
yes it is in the same directoryblobblob
use a full path and show an exact error, pleaseMrSmile
it just say fatal error and the line of the code that has the readImageblobblob
@blobblob it is very important to show the exact error if you want to get a help :)MrSmile

1 Answers

0
votes

This is typical in Imagick with PDF files. PHP does not use the same ENV variables as the OS where Imagemagick gets them.

The Ghostscript library used in the Imagemagick delegates.xml file must be changed to replace gs with the full path to gs, where it says: command="&quot;gs&quot;

  <delegate decode="ps" encode="pdf" mode="bi" command="&quot;gs&quot; -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>

Similarly in the other lines that use gs