Running libreoffice from php exec()

2019-02-08 16:10发布

I have installed libreoffice headless on a freebsd-server with apache in order to convert documents programmatically (e.g. odt->pdf). It works from the command line! But my goal is to be able to do it from php. This demands that the web-user (www) can run libreoffice. But it cannot.

When running libreoffice as my own user, I get:

%libreoffice --headless -convert-to pdf Litteraturundervisningogit.doc
javaPathHelper: not found #This should not be a problem, says people on the net.
convert /usr/home/bundsgaard.net/www/jeppe/foredrag/Litteraturundervisningogit.doc ->
 /usr/home/bundsgaard.net/www/jeppe/foredrag/Litteraturundervisningogit.pdf using writer_pdf_Export
%

If I try the same command as root, it does not work. The same is the problem with the www-user from php:

sp# libreoffice --headless -convert-to pdf Litteraturundervisningogit.doc
javaPathHelper: not found
sp#

The problem is that I do not get any information from libreoffice, thus I don't know why libreoffice does not want to run as other users than my own.

My question is: How do I give the www-user permission to run libreoffice via exec() in php?

6条回答
狗以群分
2楼-- · 2019-02-08 16:26

I was having the same problem and yes (thanks Wrikken) after prepending /usr/local/sbin to the PATH environment variable I was able to run libreoffice as www-data under apache.

查看更多
乱世女痞
3楼-- · 2019-02-08 16:29

I managed to fix this issue with a quick export HOME=/tmp before running the convert command, this gives libreoffice somewhere writable to work it's magic.

查看更多
对你真心纯属浪费
4楼-- · 2019-02-08 16:32

Not strictly an answer, but rather than using PHP's exec, you might consider using PUNO, a PHP5 module that provides access to the OpenOffice.org UNO Programming API.

查看更多
看我几分像从前
5楼-- · 2019-02-08 16:40

I'll recomend put config path first you run exec() or shell_exec();

IE:

// Vars
putenv('PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin');
putenv('HOME=' . $outputdir); 

$outputdir = chmod 777 And the same forlder from libreoffice command "--outdir"

查看更多
Lonely孤独者°
6楼-- · 2019-02-08 16:41

About Universal Network Objects (UNO), there are some "plug-and-play" final-user tools, see Docvert and JODConverter (jODconverter and pyODconverter). All of then can be called as web-service or exec by PHP.

查看更多
登录 后发表回答