alexgindre
alexgindre

Reputation: 243

PHP and Apache : shell_exec(wkhtmltopdf with xvfb) command doesn't work

I try to run the wkhtmltopdf (0.11.0 rc1) with php (5.4.2) on apache (2.4.2). When I try to launch wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1, I can find my pdf. Here my php code

<?php
    $cmd= '/usr/bin/wkhtmltopdf-i386 http://www.google.com google.pdf 2>&1';
    $ret = shell_exec($cmd);
    echo $ret;
?>

It works with apache and as command line php test.php. Because my target page contains many images and some "heavy" js charts. I have got a Segmentation Fault with the wkhtmltopdf command when I try to turn it into pdf. The only way to make it work is to use xvfb as X11 emulator. The code looks like this :

<?php
    $cmd= '/usr/bin/xvfb-run  /usr/bin/wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1';
    $ret = shell_exec($cmd);
    echo $ret;
?>

This script works with the command line php test.php but it doesn't work with apache. If I take a look into the apache's process with htop, I can see that there are two process (with php test.php) :

When I launch with apache I have only xvfb process. It finish by a timeout from apache because it's waiting the wkhtmltopdf process.

I can make it works with apache (2.2.21) and php (5.3.10).

Is there something that I'm missing ? Maybe something in the apache's config-files ?

Upvotes: 1

Views: 3740

Answers (3)

P. R. Ribeiro
P. R. Ribeiro

Reputation: 3029

I was having the same problem. I was using the exec function, but the same applies to shell_exec. The function execution was disabled in php.ini.

SOLUTION: Remove the shell_exec string from the disable_functions at php.ini file.

Upvotes: 1

alpera
alpera

Reputation: 509

it's mostly because of ownership and permissions, try

su www-data (for debian)
php test.php

you'll probably see the error.

Upvotes: 0

jimp
jimp

Reputation: 17477

I am not sure why your second version is not callable from Apache (must not be using the same shell, since shell_exec uses a shell?), but as a work-around could you (from Apache PHP) shell_exec("php test.php"); and get your intended result?

Perhaps also try one of the other process execution functions such as pcntl_exec.

Upvotes: 0

Related Questions