php libreoffice shell_exec not working

Mohammed Sufian picture Mohammed Sufian · Apr 9, 2014 · Viewed 8.3k times · Source

I am trying to convert .docx file to .html using php shell_exec in CentOS 6.5

My php code:

 $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:

javaldx: Could not find a Java Runtime Environment! 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)`

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/.

Answer

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:

1.- First check that you don't have disabled shell_exec in php.ini, and open_basedir don't restrict your access folders.

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

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:

[Java framework] Error in function createSettingsDocument (elements.cxx). javaldx failed! Warning: failed to read path from javaldx terminate called after throwing an instance of 'com::sun::star::uno::RuntimeException'

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

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.

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