WkHTMLtoPDF 0.12.2.1 PHP exec xvfb-run: Error: xauth command not found
Asked Answered
C

2

7

I want to generate a PDF from a URL, so I execute the command by WkHTMLtoPDF as below:

/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/local/bin/wkhtmltopdf http://www.google.com /tmp/google.pdf  2>&1

The above command works fine on Terminal, But when I invoke the command inside PHP failed! And show me an error message as below:

array(2) { 
    [0]=> string(27) "which: no xauth in ((null))"
    [1]=> string(40) "xvfb-run: error: xauth command not found"
}

I don't know how to resolve this issue! Anyone can help me on this, my OS environment as below:

  • OS: CentOS release 6.6
  • wkhtmltopdf version: 0.12.2.1
  • nginx version: nginx/1.6.3

My PHP code as below:

<php
    $var = array();
    $res = 0;

    $cmd = '/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/local/bin/wkhtmltopdf http://www.google.com /tmp/google.pdf  2>&1';

    exec($cmd, $var, $res);

    echo $cmd.'&lt;br /&gt;';
    var_dump ($var);
?>
Cankered answered 30/5, 2015 at 5:4 Comment(8)
Checked if safe-mode is disabled?Gauze
You mean safe_mode = Off? I checked the safe_mode is Off in php.iniCankered
You tried to run just /usr/local/bin/wkhtmltopdf http://www.google.com /tmp/google.pdf?Gauze
@joaoBeno Yes,I tried to run the command without xvfb-run inside PHP and failed! But succeed on command line.Cankered
@joaoBeno I think this is a permission issue? How to grant the permission to nobody user to run wkhtmltopdf?Cankered
What was the error when you ran it without xvfb-run?Gauze
@joaoBeno Thanks you very much, You saved me to fixed this issue, please see my answer :)Cankered
Be welcome! Please mark your answer as correct!Gauze
C
5

For CentOS PHP environment the WkHTMLtoPDF tool not need xvfb-run to exec the command, But for Ubuntu PHP environment need xvfb-run to exec the command! I had revised my code as below and the issues was resolved:

$osName = 'lsb_release -d 2>&1';
exec('lsb_release -d', $osName);
$isCentOS   = strrpos(strtolower($osName[0]), 'centos'); 

$cmd = '/usr/local/bin/wkhtmltopdf http://www.google.com /tmp/google.pdf  2>&1';
if ($isCentOS === false) {
     $cmd = '/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/local/bin/wkhtmltopdf http://www.google.com /tmp/google.pdf  2>&1';
} 

The issues is currently resolved and Thanks @joaoBeno saved me for fixed this issue~~

Cankered answered 31/5, 2015 at 4:1 Comment(1)
thank you. on my case, i'm running it on cron together with xvfb, upon checking the error it says "wkhtmltoimage command not found", i just modified my command from "wkhtmltoimage" to "/usr/local/bin/wkhtmltoimage"Harlie
S
1

If you are using PHP-FPM, by default environmental variables are not inherited into a worker process. That's why xauth cannot be found in environment variable PATH. To fix this, you may set php-fpm's config file e.g. /etc/php-fpm.d/www.conf, usually in section [www]:

  1. either uncomment the line:

    ;clear_env = no

  2. or add new line:

    env[PATH] = '/usr/local/bin:/usr/bin:/bin'

Surmount answered 11/11, 2017 at 22:21 Comment(1)
If anyone is here, I am using PHP in docker, and I had to install xauth. then it solved the issue.Electrodynamic

© 2022 - 2024 — McMap. All rights reserved.