5

An issue I discover today is similar to this unanswered problem; though not the same, it may have the same cause.

I'm rendering SVG files using inkscape, as either PNG or PDF. For the most part I intend to use Gearman to render these in the background, but for now I am creating some thumbnails inside a PHP/Apache process. It seems that if inkscape is called (via PHP's exec) inside an Apache process, it cannot find the fonts it needs to render. Thus, the graphic elements render fine, but any text elements are not drawn in the PNG output.

I suspect that the CLI environment from inside Apache is different to my usual bash console in a way that means fonts cannot be seen. I'm on OS X 10.6.8. Any ideas?

Edit: following on from comments, I've captured php -i inside both Apache and Gearman, and diffed the first against the second (so in theory applying the diff would make it work). The result is here.

Edit 2: I've tried convert -list font in both environments using system - no differences at all.

Community
  • 1
  • 1
halfer
  • 19,824
  • 17
  • 99
  • 186
  • I have solved this problem using a blocking high-priority task in Gearman, which is my preferred solution anyway - as it will help me keep a lid on the number of concurrent inkscape processes. But I still think this question is of great interest, so would encourage anyone with ideas to post 'em! Bounty forthcoming when the machine lets me. – halfer Apr 01 '12 at 21:43
  • Failing to find external files sounds like a path problem. Have you looked at the environment variables as seen by the invoked process (maybe just exec a php script that prints out `$_ENV` or calls `phpinfo()`)? – DCoder Apr 07 '12 at 13:37
  • Thanks @DCoder. I'd normally consider this a file permissions issue, but on the other hand I'm running the Gearman process under the www user also (same as Apache). I'll try dumping environment stuff inside Apache to see if there's something awry - good idea. – halfer Apr 07 '12 at 14:03
  • @DCoder - I've added a diff for `php -i`, and will also produce one for `convert -list font` to see if that has any trouble inside Apache/PHP. – halfer Apr 07 '12 at 16:09
  • That certainly has a lot of differences, especially `PATH` and `PWD`. `exec` doesn't give you much control over the invoked process, `proc_open` might be a better option: http://us.php.net/manual/en/function.proc-open.php – DCoder Apr 07 '12 at 16:14
  • Thanks @DCoder! That led me to it... it was the `HOME` env var. If you'd like to copy your last comment into an answer, you can have the points. (Additional tip of the hat if you've any ideas why - I am wondering if it is about access to ~/.fontconfig/ or maybe ~/.inkscape-etc/). – halfer Apr 07 '12 at 16:37

2 Answers2

3

As was determined in the comments above, this was caused by an environmental difference - the HOME env var was set differently inside the executed process. Using proc_open instead of simple exec gave more precise control over said process and explicitly setting that env var solved the issue.

DCoder
  • 12,962
  • 4
  • 40
  • 62
  • `exec("HOME=/path/to/home /usr/bin/inkscape")` also works. I wonder why system fonts don't work with home unset though. – qwazix Apr 21 '17 at 07:32
1

For the record, here's the usage of proc_open that helped fix this issue:

$command = "{$exec} --without-gui {$params} {$file} {$redirect}";
$return = -1;
// Comment this out for now
//exec($command, self::$output, $return);

$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "/dev/null", "a")
);
$pipes = array();
$env = array(
    // Try additional stuff here, but culprit was:
    'HOME' => '/Users/jon',
);
$resource = proc_open(
    $command,
    $descriptorspec,
    $pipes,
    $cwd = null,
    $env
);
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Thank you for sharing this part. Just one more observation: are the arguments in the command properly escaped (`escapeshellargs`)? – DCoder Apr 07 '12 at 16:45
  • Actually, I haven't; good question. Should I use that even though I'm not taking any arguments from user input? – halfer Apr 07 '12 at 16:51
  • I run all arguments through it out of habit. If you're not taking any user input (your filenames are autogenerated and sure not to contain iffy stuff like spaces), you're probably safe as it is. – DCoder Apr 07 '12 at 16:54
  • Yes, all paths have no spaces, and file leafnames are md5 hashes. But it's a good idea nonetheless - I think I will do the same. – halfer Apr 07 '12 at 16:57