2

I'm in trouble and that much confused about a php shell_exec command. When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.

Here's the command :

/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"

When I lauch this from a terminal :

$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)                                               
Done      

But from my php script :

// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..

I've test the output of shell_exec, it's empty.

The log :

Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64

No permission pb in the /tmp directory :

$ ls -la /tmp
total 448
drwxrwxrwt 16 root  root    4096 mars  12 21:51 .
../..

I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.

For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions. I don't understand anything ...

Thanks for any help, I'm loosing my mind. David.

David
  • 755
  • 1
  • 9
  • 22

5 Answers5

4

I've found the solution here : Executing wkhtmltopdf from PHP fails

Thanks to Krzychu.

First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :

$no_output = shell_exec($command);
echo $no_output; // nothing

$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"

The solution :

  1. Not use the wkhtmltopdf ubuntu package (0.9.9-4)

  2. Use the official package from the Wkhtmltopdf download page

So no need to install xvfb ! (I've seen this advice many times)

Community
  • 1
  • 1
David
  • 755
  • 1
  • 9
  • 22
2

Looks like a user's permissions issue.

When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.

When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.

How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.

Greg Kelesidis
  • 1,069
  • 14
  • 20
1

I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.

this:

$output = shell_exec($command . ' 2>&1');

instead of:

$output = shell_exec($command);

No idea why but it works and I'm grateful.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.

s3v3n
  • 8,203
  • 5
  • 42
  • 56
  • 1
    This tric do not change the point. I've succed to get the error message with the 2>&1 at the end of the commande executed : `$output = shell_exec($commande. " 2>&1"); echo $output;` I get "cannot connect to X server". – David Mar 15 '15 at 10:25
  • I'm glad you found the solution. I was thinking that you're not allowed to use that function and error reporting would turn "complain mode" on so you'd see something like this: `shell_exec() has been disabled for security reasons on line 1`. – s3v3n Mar 15 '15 at 12:34
0

I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.

I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

Daniel Resch
  • 584
  • 3
  • 12