1

I'm trying to use ImageMagick in PHP to convert PDF to images. Initializing the constructor gives me a "delegate error, could not find the specified file".

Please note, running convert from command line does work. It successfully converts all pages of PDF into images per page.

Things I've tried:

  • Uninstalling ImageMagick and Ghostscript, rebooting, installing Ghostscript ( 32 ), rebooting, installing ImageMagick, rebooting

  • Adding path to Ghostscript folder to PATH variable in windows and reboot ( it's currently added when I check )

  • Running the same exact PHP script on a server where ImageMagick is properly configured and the error is not there - so the php script should not be the issue?

Let me know if there is more info I should provide. I've searched for solutions for the past 2 hours, all out of ideas now.

Thank you.

Also, I'm running on Windows 10, XAMPP.


delegates.xml in ImageMagick

<?xml version="1.0"?>
<!DOCTYPE delegatemap [
<!ELEMENT delegatemap (delegate)+>
<!ELEMENT delegate (#PCDATA)>
<!ATTLIST delegate decode CDATA #IMPLIED>
<!ATTLIST delegate encode CDATA #IMPLIED>
<!ATTLIST delegate mode CDATA #IMPLIED>
<!ATTLIST delegate spawn CDATA #IMPLIED>
<!ATTLIST delegate stealth CDATA #IMPLIED>
<!ATTLIST delegate thread-support CDATA #IMPLIED>
<!ATTLIST delegate command CDATA #REQUIRED>
]>
<!--
  Delegate command file.

  Commands which specify

    decode="in_format" encode="out_format"

  specify the rules for converting from in_format to out_format These
  rules may be used to translate directly between formats.

  Commands which specify only

    decode="in_format"

  specify the rules for converting from in_format to some format that
  ImageMagick will automatically recognize. These rules are used to
  decode formats.

  Commands which specify only

   encode="out_format"

  specify the rules for an "encoder" which may accept any input format.

  For delegates other than ps:alpha, ps:color, ps:mono, and mpeg-encode the
  substitution rules are as follows:

    %i  input image filename
    %o  output image filename
    %u  unique temporary filename
    %#  input image signature
    %b  image file size
    %c  input image comment
    %g  image geometry
    %h  image rows (height)
    %k  input image number colors
    %l  image label
    %m  input image format
    %p  page number
    %q  input image depth
    %s  scene number
    %w  image columns (width)
    %x  input image x resolution
    %y  input image y resolution

-->
<delegatemap>
  <delegate decode="bpg" command="cmd.exe /c (&quot;bpgdec&quot; -b 16 -o &quot;%o.png&quot; &quot;%i&quot;) &amp; (move &quot;%o.png&quot; &quot;%o&quot; >nul)"/>
  <delegate decode="png" encode="bpg" command="&quot;bpgenc&quot; -b 12 -q %~ -o &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="browse" stealth="True" spawn="True" command="cmd.exe /c start &quot;&quot; http://www.imagemagick.org/"/>
  <delegate decode="dng:decode" stealth="True" command="dcraw.exe -6 -W -O &quot;%u.ppm&quot; &quot;%i&quot;"/>
  <delegate decode="dot" command="dot -Tps &quot;%i&quot; -o &quot;%o&quot;"/>
  <delegate decode="dvi" command="dvips -q -o &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="edit" stealth="True" command="notepad &quot;%o&quot;"/>
  <delegate decode="eps" encode="pdf" mode="bi" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -sDEVICE=pdfwrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
  <delegate decode="eps" encode="ps" mode="bi" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=ps2write &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
  <delegate decode="hpg" command="hp2xx -q -m eps -f &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="hpgl" command="hp2xx -q -m eps -f &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="htm" command="html2ps -U -o &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="html" command="html2ps -U -o &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="jxr" command="cmd.exe /c (move &quot;%i&quot; &quot;%i.jxr&quot; >nul) &amp; (&quot;JXRDecApp.exe&quot; -i &quot;%i.jxr&quot; -o &quot;%o.pnm&quot;) &amp; (move &quot;%i.jxr&quot; &quot;%i&quot; >nul) &amp; (move &quot;%o.pnm&quot; &quot;%o&quot; >nul)"/>
  <delegate decode="mpeg:decode" command="&quot;ffmpeg.exe&quot; -nostdin -v -1 -i &quot;%i&quot; -vframes %S -vcodec pam -an -f rawvideo -y &quot;%u.pam&quot;"/>
  <delegate decode="pcl:cmyk" stealth="True" command="&quot;pcl6.exe&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pamcmyk32&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;"/>
  <delegate decode="pcl:color" stealth="True" command="&quot;pcl6.exe&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ppmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;"/>
  <delegate decode="pcl:mono" stealth="True" command="&quot;pcl6.exe&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pbmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;"/>
  <delegate decode="pdf" encode="eps" mode="bi" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -sDEVICE=eps2write -sPDFPassword=&quot;%a&quot; &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
  <delegate decode="pdf" encode="ps" mode="bi" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=ps2write -sPDFPassword=&quot;%a&quot; &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
  <delegate decode="pgp" command="pgpv -fq &quot;%i&quot;"/>
  <delegate decode="png" encode="launch" spawn="True" mode="encode" command="imdisplay &quot;%i&quot;" />
  <delegate decode="png" encode="show" spawn="True" mode="encode" command="imdisplay &quot;%i&quot;" />
  <delegate decode="png" encode="win" spawn="True" mode="encode" command="imdisplay &quot;%i&quot;" />
  <delegate decode="pnm" encode="ilbm" mode="encode" command="ppmtoilbm -24if &quot;%i&quot; &gt; &quot;%o&quot;"/>
  <delegate decode="pnm" encode="jxr" command="cmd.exe /c (move &quot;%i&quot; &quot;%i.pnm&quot; >nul) &amp; (&quot;JXREncApp.exe&quot; -i &quot;%i.pnm&quot; -o &quot;%o.jxr&quot;) &amp; (move &quot;%i.pnm&quot; &quot;%i&quot; >nul) &amp; (move &quot;%o.jxr&quot; &quot;%o&quot; >nul)"/>
  <delegate decode="pnm" encode="wdp" command="cmd.exe /c (move &quot;%i&quot; &quot;%i.pnm&quot; >nul) &amp; (&quot;JXREncApp.exe&quot; -i &quot;%i.pnm&quot; -o &quot;%o.jxr&quot;) &amp; (move &quot;%i.pnm&quot; &quot;%i&quot; >nul) &amp; (move &quot;%o.jxr&quot; &quot;%o&quot; >nul)"/>
  <delegate decode="ps:alpha" stealth="True" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>
  <delegate decode="ps:cmyk" stealth="True" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pamcmyk32&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>
  <delegate decode="ps:color" stealth="True" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>
  <delegate decode="ps" encode="eps" mode="bi" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=eps2write &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
  <delegate decode="ps" encode="pdf" mode="bi" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=pdfwrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
  <delegate decode="ps:mono" stealth="True" command="&quot;@PSDelegate@&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>
  <delegate decode="shtml" command="html2ps -U -o &quot;%o&quot; &quot;%i&quot;"/>
  <delegate decode="svg" command="&quot;rsvg-convert&quot; -o &quot;%o&quot; &quot;%i&quot;"/>
  <!-- Remove the extra space in - -export in the line below when you want to use inkscape -->
  <!--<delegate decode="svg:decode" stealth="True" command="&quot;inkscape&quot; &quot;%s&quot; - -export-eps=&quot;%s&quot; - -export-dpi=&quot;%s&quot; - -export-background=&quot;%s&quot; - -export-background-opacity=&quot;%s&quot; &gt; &quot;%s&quot; 2&gt;&amp;1"/>-->
  <delegate decode="wdp" command="cmd.exe /c (move &quot;%i&quot; &quot;%i.jxr&quot; >nul) &amp; (&quot;JXRDecApp.exe&quot; -i &quot;%i.jxr&quot; -o &quot;%o.pnm&quot;) &amp; (move &quot;%i.jxr&quot; &quot;%i&quot; >nul) &amp; (move &quot;%o.pnm&quot; &quot;%o&quot; >nul)"/>
  <delegate decode="xps:cmyk" stealth="True" command="&quot;gxps.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pamcmyk32&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;"/>
  <delegate decode="xps:color" stealth="True" command="&quot;gxps.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;"/>
  <delegate decode="xps:mono" stealth="True" command="&quot;gxps.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pbmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;"/>
  <delegate encode="mpeg:encode" stealth="True" command="&quot;ffmpeg.exe&quot; -nostdin -v -1 -i &quot;%M%%d.jpg&quot; &quot;%u.%m&quot;"/>
</delegatemap>

Error received:

object(ImagickException)#805 (7) {
  ["message":protected]=>
  string(95) "PDFDelegateFailed `The system cannot find the file specified.
' @ error/pdf.c/ReadPDFImage/801"
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(415)
  ["file":protected]=>
  string(70) "D:\xampp\htdocs\pdfshare\wp-content\plugins\pdfshare\lib\class.pdf.php"
  ["line":protected]=>
  int(57)
  ["trace":"Exception":private]=>
  array(6) {
    [0]=>
    array(6) {
      ["file"]=>
      string(70) "D:\xampp\htdocs\pdfshare\wp-content\plugins\pdfshare\lib\class.pdf.php"
      ["line"]=>
      int(57)
      ["function"]=>
      string(11) "__construct"
      ["class"]=>
      string(7) "Imagick"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(1) {
        [0]=>
        string(62) "D:\xampp\htdocs\pdfshare\wp-content\uploads\2019\05\2А-74.pdf"
      }
    }
    [1]=>
    array(6) {
      ["file"]=>
      string(65) "D:\xampp\htdocs\pdfshare\wp-content\plugins\pdfshare\inc\ajax.php"
      ["line"]=>
      int(94)
      ["function"]=>
      string(15) "generate_images"
      ["class"]=>
      string(12) "PDFShare_PDF"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(0) {
      }
    }
    [2]=>
    array(4) {
      ["file"]=>
      string(54) "D:\xampp\htdocs\pdfshare\wp-includes\class-wp-hook.php"
      ["line"]=>
      int(286)
      ["function"]=>
      string(30) "PDFShare\AJAX\upload_pdf_entry"
      ["args"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
    }
    [3]=>
    array(6) {
      ["file"]=>
      string(54) "D:\xampp\htdocs\pdfshare\wp-includes\class-wp-hook.php"
      ["line"]=>
      int(310)
      ["function"]=>
      string(13) "apply_filters"
      ["class"]=>
      string(7) "WP_Hook"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(2) {
        [0]=>
        string(0) ""
        [1]=>
        array(1) {
          [0]=>
          string(0) ""
        }
      }
    }
    [4]=>
    array(6) {
      ["file"]=>
      string(47) "D:\xampp\htdocs\pdfshare\wp-includes\plugin.php"
      ["line"]=>
      int(465)
      ["function"]=>
      string(9) "do_action"
      ["class"]=>
      string(7) "WP_Hook"
      ["type"]=>
      string(2) "->"
      ["args"]=>
      array(1) {
        [0]=>
        array(1) {
          [0]=>
          string(0) ""
        }
      }
    }
    [5]=>
    array(4) {
      ["file"]=>
      string(48) "D:\xampp\htdocs\pdfshare\wp-admin\admin-ajax.php"
      ["line"]=>
      int(167)
      ["function"]=>
      string(9) "do_action"
      ["args"]=>
      array(1) {
        [0]=>
        string(33) "wp_ajax_pdfshare_upload_pdf_entry"
      }
    }
  }
  ["previous":"Exception":private]=>
  NULL
}
KenS
  • 30,202
  • 3
  • 34
  • 51
Tagr
  • 13
  • 4
  • I don't think PHP uses the system PATH variable at all. I think you need to find your ImageMagick `delegates.xml` file and look for the line(s) containing `pdf` and `gs` (ghostscript) and put the full path to wherever your `gs` binary lives in there. – Mark Setchell May 10 '19 at 19:01
  • Run `php -m`; does it show you the module is loaded? Being able to use `convert` from CLI is not relevant. Also provide a minimal PHP script that will reproduce the problem for you. – miken32 May 10 '19 at 19:01
  • @miken32 yes, it does show that the module is loaded. The PHP script is just the constructor, throws an exception with that error message: `try{ $imagick = new Imagick( $pdf_path ); } catch ( Exception $ex ) { var_dump( $ex ); exit; }` – Tagr May 10 '19 at 19:17
  • @MarkSetchell Actually, looking in `delegates.xml` I cannot find a line that contains `gs` at all. There are some that contain `pdf` but not with `gs`. I'll add the code I see in the original question now. Thank you. – Tagr May 10 '19 at 19:22
  • Showing the output from that `var_dump` statement might shed some more light on things. – miken32 May 10 '19 at 19:23
  • @miken32 Added in the question. – Tagr May 10 '19 at 19:31
  • 1
    Is `PSDelegate` set somewhere in there? – Mark Setchell May 10 '19 at 19:33
  • Well that's a whole different error message than you originally said. – miken32 May 10 '19 at 19:37
  • Possible duplicate of [PHP: Uncaught ImagickException: PDFDelegateFailed](https://stackoverflow.com/questions/47969371/php-uncaught-imagickexception-pdfdelegatefailed) – miken32 May 10 '19 at 19:37
  • @miken32 I've seen the duplicate question, and already tried the solutions offered there, As mentioned in the question, reinstalling or adding folder to PATH variable did not help. – Tagr May 10 '19 at 19:40
  • @MarkSetchell Sorry, set where? I can only see `PSDelegate` in a few lines, in `command` attribute. Also, updated the question with the full `delegates.xml` content. – Tagr May 10 '19 at 19:41
  • I would make a copy of your `delegates.xml` and save it so you can revert to it. Then edit the original and replace `@PSDelegate@` with `C:\gs-3.17\bin\gs.exe` or wherever your `gs` is. – Mark Setchell May 10 '19 at 19:46
  • I'm surprised if the PHP extension relies on any external configuration files, but I'm not super familiar with IM's PHP extension. If it is using that XML file, you could try here: https://www.imagemagick.org/discourse-server/viewtopic.php?t=15056#p76579. Apparently on Windows it relies on a registry entry to look up `PSDelegate` which isn't always accurate. – miken32 May 10 '19 at 19:47
  • I do not know what PSDelegate is? Is it a placeholder. Your delegates.xml file should be showing `command=""gs"` (for PDF/PS/EPS lines), where gs is Ghostscript. On PHP, you may need to provide the full path to Ghostscript as `command=""path_to/gs"`. One other issue is that you may need to enable permissions for using PDF in Imagemagick policy.xml file. See https://stackoverflow.com/questions/52861946/imagemagick-not-authorized-to-convert-pdf-to-an-image/52863413#52863413. What is your full Imagemagick version from `convert -version` – fmw42 May 10 '19 at 20:33
  • Continued: On Windows those lines in the delegates.xml file may need to be command=""gs.exe" or command=""path_to/gs.exe". But also check your policy.xml file as I suggested above with the link to enabling reading of PDF, etc. – fmw42 May 10 '19 at 20:43
  • @fmw42 Updated delegates.xml file with full paths to ghostscript executable, changed policy to give all permissions for coder and delegate domains. Still no luck. So frustrating. – Tagr May 11 '19 at 12:33
  • Maybe you could edit your question and add in the lines you have modified in your `delegates.xml` file? Also, have you mixed 64-bit and 32-bit versions of ImageMagick and Ghostscript? – Mark Setchell May 12 '19 at 09:46

0 Answers0