简体   繁体   中英

Running libreoffice from php exec()

I have installed libreoffice headless on a freebsd-server with apache in order to convert documents programmatically (eg 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?

在设置转换命令之前,我设法通过快速export HOME=/tmp解决了这个问题,这使得libreoffice可以在某个地方写入来实现它的魔力。

不是严格的答案,而是使用PUNO ,而不是使用PHP的exec,可以考虑使用PUNO ,这是一个PHP5模块,可以访问OpenOffice.org UNO编程API。

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"

在将/usr/local/sbin到PATH环境变量之后,我遇到了同样的问题并且是(感谢Wrikken)我能够在apache下运行libreoffice作为www-data。

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.

This works for me.

Make sure you have installed java RE, for example in ubuntu:

apt-get install default-jre

First, find the location of your libreoffice

$ which libreoffice
/usr/bin/libreoffice

Include the folder location in the PATH, and also set the HOME var by adding these lines:

putenv('PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin');
putenv('HOME=/tmp'); 
system("libreoffice .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM