2

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

unloco
  • 6,928
  • 2
  • 47
  • 58
  • I am not sure but this could be the case, when running from terminal you are accessing the inkscape as system user but when running the command from the php so php might not have the permission to invoke the application, So you can also try to run it as Administrator, and you can invoke it from php as admin by following runas /user:Administrator "cmdName parameters" – BlackXero Oct 05 '19 at 20:13
  • Converting from pdf to svg worked fine, that's why i didn't suspect it was a permission problem – unloco Oct 05 '19 at 20:18
  • 1
    A common problem is with environment variables. Make sure your script's `PATH` isn't handicapped somehow, so that it can find the command `inkscape` and it's dependencies. If you are on Windows, you probably need to set the Inkscape path in the global `PATH` instead of your user's `PATH` and then restart the web server in order for it to work. – Havenard Oct 05 '19 at 22:47
  • @Havenard conversion from pdf to svg worked correctly. I'm using a ubuntu container to run my apache server – unloco Oct 06 '19 at 13:08
  • 1
    Yeah, I don't know then, it could be anything, you will have to debug the command as it runs in the PHP environment to figure what's going on. Add a `2>&1` to the command line so that you can see what it is outputting as errors, this might give you a clue. – Havenard Oct 06 '19 at 16:14
  • Thank you, it's a great tip to use `2>&1`, I added the output of the command when ran from php – unloco Oct 07 '19 at 14:17
  • I finally found the solution http://www.inkscapeforum.com/viewtopic.php?t=19303 – unloco Oct 07 '19 at 14:29
  • 1
    So you amended the 'PATH' environement variable? Did you add the '/usr/local/bin' part? That would suggest that ghostscript is installed under '/usr/local' and not, as expected if installed via distribution-packages, under '/usr'. Would be nice to know the 'why' of the solution. – Tom Regner Oct 08 '19 at 10:27
  • @TomRegner You are right, the solution in the link doesn't make a lot of sense. In fact, the most minimal solution is to prepend `PATH=/usr/bin` to the inkscape command. gs is in `/usr/bin/ghostscript` – unloco Oct 09 '19 at 11:19
  • Post it if you'd like, I will upvote and accept – unloco Oct 09 '19 at 11:22

2 Answers2

4

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.

Tom Regner
  • 6,856
  • 4
  • 32
  • 47
1

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.

Daniklad
  • 945
  • 8
  • 10
  • I tried that and it worked correctly both with terminal and php shell but not inside the script – unloco Oct 06 '19 at 13:08