Page 1 of 1

Problem converting pdf to jpg with php

Posted: 2019-01-30T11:12:54-07:00
by nbz
Hi,

I have installed ghostscript 9.26 on a 64 bit windows 10.
I also installed installed Imagemagick 7.0.8-Q16.

Converting a pdf into a jpg file works without a problem using command line.
However, I cannot convert a pdf using php code.

I tried running the following code:

$out = shell_exec("convert mks_test.pdf mks_thumb.jpg 1 2>&1");
echo $out;

The get the following response:
convert: PDFDelegateFailed `[ghostscript library 9.26] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" "-sOutputFile=C:/WINDOWS/TEMP/magick-1421694Y3jzHSeGVN%d" "-fC:/WINDOWS/TEMP/magick-14216G5hIgg9jUO9G" "-fC:/WINDOWS/TEMP/magick-14216h0tJnGVaHpD6": (null)' @ error/pdf.c/ReadPDFImage/795. convert: no images defined `mks_thumb.jpg' @ error/convert.c/ConvertImageCommand/3300.

Can anyone please suggest what might be the problem? I've been searching online for clues for hours.

Thanks

Re: Problem converting pdf to jpg with php

Posted: 2019-01-30T12:42:47-07:00
by fmw42
PHP does not use the same environment variables as your OS. So one issue that may happen is that you need to edit your delegates.xml file for all decode="ps... lines to put the full path to gs. For example

Change

<delegate decode="ps:alpha" stealth="True" command="&quot;gs&quot; -sstdout=%%stderr -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;"/>

to

<delegate decode="ps:alpha" stealth="True" command="&quot;fullpathto/gs&quot; -sstdout=%%stderr -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;"/>

On Windows, you may need to use fullpathto\gs rather than fullpathto/gs. I do not use Windows, so am not sure.

Re: Problem converting pdf to jpg with php

Posted: 2019-01-31T02:17:50-07:00
by nbz
Hi,

Thanks for the tips. Sadly, it's still not working.
I replaced the @PSDelegate@ variable in the edligates.xml file, to C:\Program Files\gs in all ps lines along with pdf and eps lines. Still no luck.
I also tried replacing with C:/Program Files/gs and it didn't work.

Of course I restarted IIS every time.

Did I do some thing wrong?
Any other ideas?

Re: Problem converting pdf to jpg with php

Posted: 2019-01-31T16:37:36-07:00
by fmw42
Be sure you have set your permissions for PDF in your policy.xml file. But I am not sure that would be the issue if it works in command line but not in PHP.

Otherwise, consult someone on setting up PHP environment variables to know where Ghostscript is located. Also check that you have only one Ghostscript on the system. For example perhaps PHP is looking to a version somewhere else than where your version was installed.

I do not know if Windows needs quotes around paths that contain spaces. That would be needed on Unix systems. Your path C:\Program Files\gs has a space between Program and Files.

Re: Problem converting pdf to jpg with php

Posted: 2019-02-01T03:16:12-07:00
by nbz
Thanks for replying.
I tried adding the following line to the policy.xml file
<policy domain="coder" rights="read | write" pattern="PDF" />
It didn't work.

I then tried adding the explicit path to the ghostscript exe like so:
&quot;C:\Program Files\gs\gs9.26\bin\gswin64.exe&quot;

I also tried (different exe file name)
&quot;C:\Program Files\gs\gs9.26\bin\gswin64c.exe&quot;

Nothing works.
I only have one ghostscript version installed.

This is driving me crazy..

Re: Problem converting pdf to jpg with php

Posted: 2019-02-01T10:13:45-07:00
by fmw42
I do not know what else to try. I am not a PHP expert. The only other thing that comes to mind is that I have heard reports that GS 9.26 does not work well in certain cases. Have you tried downgrading to 9.23, which I used for quite a while with no issues. This is not like the issue since you can run in command line mode. But it is all that comes to mind.