9
5
I installed libreoffice-headless and can convert documents when logged on into the shell as a normal user.
[root@desktop ~]# yum install libreoffice-headless
[root@desktop ~]# yum install libreoffice-writer
[root@desktop ~]# su NotionCommotion
sh-4.1$ /usr/bin/libreoffice --headless -convert-to pdf --outdir /tmp/ayb /tmp/ayb/document_34.doc
convert /tmp/ayb/document_34.doc -> /tmp/ayb/document_34.pdf using writer_pdf_Export
I now wish to do the same thing, but using PHP and therefore as user apache, however, the following will not convert the file.
<?php
shell_exec('/usr/bin/libreoffice --headless -convert-to pdf --outdir /tmp/ayb /tmp/ayb/document_34.doc');
?>
In an attempt to troubleshoot, I ran the same command through the shell as user apache, but still it will not convert the file:
[root@desktop ~]# su -s /bin/sh apache -c "/usr/bin/libreoffice --headless -convert-to pdf --outdir /tmp/ayb /tmp/ayb/document_34.doc"
Apache unlike normal users doesn't have a home, and I recall hearing I might need to specify a home using HOME=/tmp/ayb
before attempting to convert, but it doesn't help (I think when using CentOS 5.8 and probably a different version of LibreOffice, it did, but am not certain).
How do I convert a file to PDF using libreoffice when running it as user apache?
Installed System:
CentOS 6.4
httpd.x86_64 2.2.15-28.el6.centos @updates
libreoffice-headless.x86_64 1:3.4.5.2-16.1.el6_3 @base
Also actual for Ubuntu (without home directory
libreoffice --headless
dies afterX11 connection rejected because of wrong authentication
message). – Stanislav Ivanov – 2017-06-09T23:01:59.780Thanks terdon, works perfect for me as well. Couple of questions. Why not keep them in /tmp, and not in the same folder as where webpages are stored (I tested it, it works). Why doesn't
export HOME=/tmp/ayb libreoffice --headless ...
work? What is the purpose ofexport
and why doesn'tHOME=/tmp/ayb; libreoffice --headless....
work? – user1032531 – 2013-08-02T19:18:16.863If you can actually write to
/tmp
you are free to do so. However, it is a good idea from a security point of vew to not give your web server access to directories that are outside/var/www
.export
exports the variable, making it available to all subsequent shells and&&
makes sure it runs only if the export was successful. I'm not sure about the details, apache can be quite finicky about permissions, it is always better to keep everything under yourwww
folder. – terdon – 2013-08-02T19:20:49.247I am mixed on using tmp. Yes, apache can write to
/tmp
, and a good thing (I think) about using it is that it automatically purges old files. But then again, I see your point about keeping everything under/var/www
. – user1032531 – 2013-08-02T19:44:57.017