简体   繁体   English

php libreoffice shell_exec不能正常工作

[英]php libreoffice shell_exec not working

I am trying to convert .docx file to .html using php shell_exec in CentOS 6.5 我想在CentOS 6.5中使用php shell_exec.docx文件转换为.html

My php code: 我的PHP代码:

 $command = "libreoffice --headless -convert-to html resume.docx 2>&1";
 $result = shell_exec($command);
 echo $result;

When I run the index.php at http://localhost/converter/ it gives me: 当我在http://localhost/converter/运行index.php ,它给了我:

javaldx: Could not find a Java Runtime Environment! javaldx:找不到Java运行时环境! Warning: failed to read path from javaldx /usr/lib64/libreoffice/program/soffice.bin X11 error: Can't open display: Set DISPLAY environment variable, use -display option or check permissions of your X-Server (See "man X" resp. "man xhost" for details)` 警告:无法从javaldx读取路径/usr/lib64/libreoffice/program/soffice.bin X11错误:无法打开显示:设置DISPLAY环境变量,使用-display选项或检查X-Server的权限(参见“man” X“resp。”man xhost“了解详情)`

while in terminal it is working perfectly: 在终端,它完美地工作:

cd /var/www/html/converter/

libreoffice --healdess -convert-to html resume.docx

here it creates resume.html in my /var/www/html/converter/ . 这里它在我的/var/www/html/converter/创建了resume.html

Hi i have the same problem, i want to convert PDF's from DOCS created with PHP, i'm using OpenSuse 12.3 with LibreOffice, tried many things, finally i detect that the error is in folder: 嗨我有同样的问题,我想从用PHP创建的DOCS转换PDF,我使用OpenSuse 12.3与LibreOffice,尝试了很多东西,最后我检测到错误在文件夹中:

1.- First check that you don't have disabled shell_exec in php.ini, and open_basedir don't restrict your access folders. 1.-首先检查你是否在php.ini中没有禁用shell_exec,open_basedir不限制你的访问文件夹。

2.- Run the command as a simple user in shell (terminal) export HOME=/tmp && soffice --headless --convert-to pdf --outdir /srv/www/htdocs/ /srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx 2.-在shell(终端)中以简单用户身份运行命令导出HOME = / tmp && soffice --headless --convert-to pdf --outdir / srv / www / htdocs / / srv / www / htdocs / Creecimientos / SIC /应用程序/根目录/ USUARIOS / 2 / 8_Pagare_CreePersonas.docx

3.- If it works, you only have to put the correct folders in your code, when i run this code in PHP, it show me a blank page, so i check the access_log of apache for any hint: 3.-如果它有效,你只需要在你的代码中放入正确的文件夹,当我在PHP中运行此代码时,它会显示一个空白页面,所以我检查apache的access_log是否有任何提示:

[Java framework] Error in function createSettingsDocument (elements.cxx). [Java framework]函数createSettingsDocument(elements.cxx)中的错误。 javaldx failed! javaldx失败了! Warning: failed to read path from javaldx terminate called after throwing an instance of 'com::sun::star::uno::RuntimeException' 警告:在抛出'com :: sun :: star :: uno :: RuntimeException'的实例后,无法读取javaldx终止时调用的路径

Note: my error was in using export HOME=/tmp, i checked that the folder in root system has 777 for tmp, but the problem was that apache don't acces to it, maybe search for a relative folder of the script, but after test many things i only put a folder with permissons for wwwrun HOME=/srv/www/htdocs/folder_with_777 注意:我的错误是使用导出HOME = / tmp,我检查根系统中的文件夹有777用于tmp,但问题是apache无法访问它,可能会搜索脚本的相关文件夹,但是经过测试很多东西我只放了一个文件夹,用于wwwrun HOME = / srv / www / htdocs / folder_with_777

This is my final code, that works.. 这是我的最终代码,有效..

<?php
     function word2pdf()
        { 
        echo "Procesando";         
       $result = shell_exec('export HOME=/srv/www/htdocs/Creecimientos/sic/ && soffice --headless --convert-to pdf --outdir /srv/www/htdocs/Creecimientos/sic/ /srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx');
       echo $result;
        }

        word2pdf();
?>

In fact, it prints: convert srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx -> /srv/www/htdocs/Creecimientos/sic//8_Pagare_CreePersonas.pdf using writer_pdf_Export, after succes. 事实上,它使用writer_pdf_Export打印:convert srv / www / htdocs / Creecimientos / sic / app / webroot / usuarios / 2 / 8_Pagare_CreePersonas.docx - > /srv/www/htdocs/Creecimientos/sic//8_Pagare_CreePersonas.pdf 。

I made other changes before in desesperate mode, but none of them solved the problem, tried to change owner to soffice wich found it witch $ ls -l $(which libreoffice), tried with 777, etc.. 我在desesperate模式之前做了其他更改,但没有一个解决了问题,试图将所有者更改为soffice,发现它是巫婆$ ls -l $(自由的libreoffice),尝试用777等等。

Most likely the user that LibreOffice is ran as, does not have a writeable home directory so LibreOffice fails to create it's config directory and then it cannot create it's config files and then fails to load Java, because it cannot write the default config. 很可能是LibreOffice运行的用户,没有可写的主目录,因此LibreOffice无法创建它的配置目录,然后它无法创建它的配置文件,然后无法加载Java,因为它无法写入默认配置。 A bit silly I know. 我知道有点傻。

Try adding this parameter: -env:UserInstallation=file:///tmp/whateverhere 尝试添加此参数: -env:UserInstallation=file:///tmp/whateverhere

/* This command will work on centos 6 /7 with installation of libreoffice headless package */
First install package on centos as : 
yum install libreoffice-headless

/* following code work to extract text format from */  
<?php  
 $result = exec("export HOME=/tmp/ && /usr/bin/libreoffice --headless   --convert-to txt:Text --outdir /tmp filePath");   
 var_dump($result); 
?>

I don't have enough reputation to comment on TD_Nijboer's answer, but the answer to his specific problem appears to be that soffice needs to be able to read & write config information somewhere. 我没有足够的声誉评论TD_Nijboer的答案,但他的具体问题的答案似乎是, soffice需要能够在某处读取和写入配置信息。 The first place it tries to do this is the libreoffice directory in ~/.config ('~' means "the current user's home directory"). 它尝试这样做的第一个地方是~/.configlibreoffice目录('〜'表示“当前用户的主目录”)。

In Debian, by default, the www-data user has the home directory /var/www , and does not have write permission there. 在Debian中,默认情况下, www-data用户具有主目录/var/www ,并且没有写入权限。

If you make sure it has permission to either create ~/.config itself, or libreoffice within an existing ~/.config , I expect it will work. 如果你确定它有权创建~/.config本身,或者在现有的~/.config创建libreoffice ,我希望它能工作。

2 things, 1st the command is soffice --headless , 2件事,第1命令是soffice --headless
2nd i have an similar javaldx error and it has to do with permission. 第二个我有一个类似的javaldx错误,它与权限有关。
when executing as root it works fine, but php executes as www-data. 当以root身份执行时它工作正常,但php作为www-data执行。

if anybody knows a good way to execute libreoffice from php please let me know. 如果有人知道从php执行libreoffice的好方法,请告诉我。
as i'm getting an error code 77 saying: 因为我收到error code 77说:

[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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