unloco
unloco

Reputation: 7320

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

Upvotes: 2

Views: 1339

Answers (2)

user594138
user594138

Reputation:

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.

Upvotes: 4

Daniklad
Daniklad

Reputation: 1079

I would guess permissions.

PHP-shell probably works because it's running under your local user.

Check which user your webserver or PHP-session is running underand check if that user may access and run inkscape. If not, add the necessary permissions.

When you run PHP in Terminal it runs under whichever user opened the terminal but when you run it in a webserver it usually runs under the user that started the webserver (there are exceptions to this though).

You can try the solutions to finding the which user php is running under in your webserver: How to check what user php is running as?

Give that user execute permissions to your inkscape folder and it should work.

Upvotes: 1

Related Questions