'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.
Solution 1:[1]
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.
Solution 2:[2]
I had the same problem and resolved it by adding two sub-folders in /var/www/ .cache and .config which are required for unoconv
Env Ubuntu 18.04 server Apache, unoconv 0.7, platform posix/linux, python 3.6.9 (default, Dec 8 2021, 21:08:43), [GCC 8.4.0], LibreOffice 6.0.7.3
user@ip-*-*-*-*:/var/www$ sudo mkdir .cache
user@ip-*-*-*-*:/var/www$ sudo chown -R www-data:www-data .cache/
user@ip-*-*-*-*:/var/www$ sudo chmod -R 744 .cache/
user@ip-*-*-*-*:/var/www$ sudo mkdir .config
user@ip-*-*-*-*:/var/www$ sudo chown -R www-data:www-data .config/
user@ip-*-*-*-*:/var/www$ sudo chmod -R 744 .config/
The home folder of www-data is /var/www/ which by default is set to the root user and unoconv can't create the required folders for cache and config.
To check your www-data $HOME
sudo cat /etc/passwd | grep www-data
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
I think that is not safe to change the permissions of /var/www/ (to www-data user and group) but is ok to add the .cache and .config folder with www-data permissions. Lets some experts confirm.
P.S. the generator return an error about the docx version but the pdf is generated
$command = 'unoconv -f pdf ' . $path.$file;
exec($command, $output);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kim Stacks |
| Solution 2 | AssyK |
