Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php libreoffice shell_exec not working

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

like image 943
Mohammed Sufian Avatar asked Apr 09 '14 20:04

Mohammed Sufian


3 Answers

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

like image 199
Henry Gabriel González Montejo Avatar answered Nov 16 '22 11:11

Henry Gabriel González Montejo


/* 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); 
?>
like image 26
Trimbak Gopalghare Avatar answered Nov 16 '22 10:11

Trimbak Gopalghare


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. A bit silly I know.

Try adding this parameter: -env:UserInstallation=file:///tmp/whateverhere

like image 4
Vlatko Šurlan Avatar answered Nov 16 '22 09:11

Vlatko Šurlan