Convert file to PDF using LibreOffice under user apache (i.e. when using PHP)
Solution 1:
There are two problems here. The first is that www-data
(the apache user) does not have a $HOME
so libreoffice cannot run if there is no $HOME
defined. The second problem is, unless you specifically set it up this way (and you really really really shouldn't), apache
does not have access to the system /tmp
directory. A web server normally runs in a restricted environment and does not have full access to the file system for very valid security reasons.
So, you need to i) give apache's user a home and ii) give it a directory it has access to to write in. So, create a tmp
directory in the same folder where you store your webpage and then run the following php
code:
<?php
shell_exec('export HOME=/tmp && libreoffice --headless -convert-to pdf --outdir ./tmp /tmp/ayb/document_34.doc');
?>
I just tested and it works perfectly on my machine. Make sure your ./tmp
has its permissions set to 777. Also, you may need to restart apache if you play aroud with it too much. It stopped working for me after a while when I made changes and I needed to restart it.
Solution 2:
I had similar problem on Debian and I solved it.
Run your command, but with strace
on the beginning, like this:strace -f -o output.txt soffice --headless --convert-to pdf (...)
This will produce huge log file with every access to the system API and its result.
In my case, somewhere near line 5000 there was something like this:open("/var/spool/libreoffice/uno_packages/cache/uno_packages", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = -1 EACCES (Permission denied)
Following this trail, I changed permissions recursively for folder /var/spool/libreoffice
to 777
.
After that, conversion started to work for every user.
Maybe you also get Permission denied
on some other file, it is handled silently, and you need to fix permissions for your user?