Search code examples
phpinkscape

PHP: use inkscape to convert eps to svg


When I convert eps to svg in the terminal using inkscape, it works fine.
But when I execute the same command using php's shell_exec, it doesn't work. (I also tried exec and system with no luck)

Sample code:

<?php

unlink('./sample.svg');
$file_path = realpath('./sample.eps');
$dest_path = getcwd() . '/sample.svg';

//# inkscape --file=sample.eps --export-plain-svg=sample.svg
// command works fine in terminal but not in php

$command = "inkscape --file=$file_path --export-plain-svg=$dest_path";
// command fails with no output (null)
$output = shell_exec($command);
var_dump($output);
var_dump(is_file('./sample.svg'));

The same command also works correctly from php shell!
I couldn't determine the cause because I couldn't check the output (it's always null)

Conversion worked fine with ai -> svg and pdf -> svg

I suspect that this is a similar issue to command works fine through terminal but not shell_exec php but what would be the solution to this case?

PS: I'm using this sample eps file for testing

Edit: I added 2>&1 to the shell_exec command and got this output

/srv/www/git/presta17_designer/eps/sample.eps:1: parser error : Start tag expected, '<' not found
%!PS-Adobe-3.0 EPSF-3.0
^
/srv/www/git/presta17_designer/eps/sample.eps:1: parser error : Start tag expected, '<' not found
%!PS-Adobe-3.0 EPSF-3.0
^

** (inkscape:717): WARNING **: 14:16:21.492: Specified document /srv/www/git/presta17_designer/eps/sample.eps cannot be opened (does not exist or not a valid SVG file)

More info:
I found the cause of the issue with the help of commenters
In shell_exec, the PATH env is empty
(var_dump(shell_exec("printenv PATH"));)
but ghostscript which is used for eps is located in /usr/bin/ghostscript
so PATH=/usr/bin should be prepended to the command for it to work properly


Solution

  • Inkscape uses ghostscript for .eps conversion, the error thrown in your case doesn't suggest it, but is indeed raised if inkscape can't find ghostscript - therefore I suggested that you check which part of the amended PATH did fix the problem.

    You found out that the minimal PATH=/usr/bin prepended to the command is sufficient to solve the problem in your case, and that is in my humble opinion indeed the preferable solution.