簡體   English   中英

從php exec()運行libreoffice

[英]Running libreoffice from php exec()

我已經在帶有apache的freebsd-server上安裝了libreoffice headless,以便以編程方式轉換文檔(例如odt-> pdf)。 它從命令行工作! 但我的目標是能夠從PHP做到這一點。 這要求web用戶(www)可以運行libreoffice。 但它不能。

當我自己的用戶運行libreoffice時,我得到:

%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
%

如果我嘗試與root相同的命令,它不起作用。 同樣是來自php的www-user的問題:

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

問題是我沒有從libreoffice獲得任何信息,因此我不知道為什么libreoffice不想像其他用戶那樣運行。

我的問題是:如何通過php中的exec()授予www-user權限來運行libreoffice?

在設置轉換命令之前,我設法通過快速export HOME=/tmp解決了這個問題,這使得libreoffice可以在某個地方寫入來實現它的魔力。

不是嚴格的答案,而是使用PUNO ,而不是使用PHP的exec,可以考慮使用PUNO ,這是一個PHP5模塊,可以訪問OpenOffice.org UNO編程API。

我建議先放置配置路徑,然后運行exec()或shell_exec();

IE:

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

$ outputdir = chmod 777來自libreoffice命令的同一個forlder“--outdir”

在將/usr/local/sbin到PATH環境變量之后,我遇到了同樣的問題並且是(感謝Wrikken)我能夠在apache下運行libreoffice作為www-data。

關於Universal Network Objects(UNO) ,有一些“即插即用”的最終用戶工具,請參閱DocvertJODConverterjODconverterpyODconverter )。 所有這些都可以被PHP稱為web-service或exec。

這適合我。

確保已安裝java RE,例如在ubuntu中:

apt-get install default-jre

首先,找到你的libreoffice的位置

$ which libreoffice
/usr/bin/libreoffice

在PATH中包含文件夾位置,並通過添加以下行來設置HOME var:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM