Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unoconv works from terminal using www-data but not from php script also as www-data

I wrote the following function in php

public static function convert($originFilePath, $outputDirPath, $toFormat)
{
    $command = 'echo $PATH & UNO_PATH=/usr/lib/libreoffice unoconv --format %s --output %s %s';
    $command = sprintf($command, $toFormat, $outputDirPath, $originFilePath);
    exec($command, $output, $result_var);

    return compact('output', 'result_var', 'outputDirPath', 'originFilePath', 'toFormat');
}

It did not generate any error message, or any pdf file as well.

In terminal, when I run the unoconv directly as www-data, I had no issues.

This is my result after execution:

2013-05-26 03:05:30 Error: Array
(
    [output] => Array
        (
            [0] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        )

    [result_var] => 1
    [outputDirPath] => /var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/2
    [originFilePath] => /var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/2/dsadas.xlsx
    [toFormat] => pdf
)

Please advise.

like image 536
Kim Stacks Avatar asked May 25 '13 09:05

Kim Stacks


2 Answers

The issue is that I am using Nginx and PHP-FPM.

In Nginx the PATH is NOT declared by default.

So there are 2 solutions.

1) you declare it in the fastcgi params for Nginx.

See here.

2) you declare it in the script using putenv() just before you run the unoconv code.

like

putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/node/bin');

I would also like to add that a certain troubleshooting method helped me to realize this problem. See here.

like image 67
Kim Stacks Avatar answered Oct 24 '22 03:10

Kim Stacks


For a possible solutions see here.

Excerpt from post...

This is what I did to make unoconv work through apache / php code running on Cent OS 6.2 (unoconv version 0.6 and LibreOffice 3.4.5.2): (This is only a workaround - root cause is not known to me)

Change apache user from /sbin/nologin to /bin/bash (This is done in /etc/passwd file) Add a new user unoconv Added a new file /etc/sudoers.d/unoconv with the following contents:

apache ALL=(unoconv) NOPASSWD: /usr/bin/unoconv (note that my unoconv program is in this location /usr/bin/unoconv - you find it using which unoconv)

Using visudo comment out the followin line (by adding a # at the start of the line)

#Defaults requiretty

Restart sshd and httpd services

Run unoconv like this with php exec() function (you would need to change the input file name and output directory):

exec('sudo -u unoconv /usr/bin/unoconv -f pdf -o bankgenerated Teacher_bulk_upload.csv');

Hope this works out for you

like image 35
Orangepill Avatar answered Oct 24 '22 04:10

Orangepill