Unoconv works with the terminal using www data, but not with a PHP script like 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 messages or any PDF file.

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

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 inform.

+6
source share
2 answers

The problem is that I use Nginx and PHP-FPM.

In Nginx, PATH is NOT declared by default.

So there are 2 solutions.

1) you declare it in fastcgi options for Nginx.

See here .

2) you declare it in a script using putenv() just before running unoconv code.

as

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 specific troubleshooting method helped me implement this problem. See here .

+5
source

For possible solutions see here .

Excerpt from the article ...

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 - the root cause is not known to me)

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

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

Using visudo, comment out the followin line (adding # at the beginning of the line)

#Defaults requiretty

Restart sshd and httpd services

Run unoconv like this with the php exec () function (you will 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 for you.

+2
source

Source: https://habr.com/ru/post/945829/


All Articles