Mohammed Sufian
Mohammed Sufian

Reputation: 1781

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

Upvotes: 8

Views: 12290

Answers (5)

Trimbak Gopalghare
Trimbak Gopalghare

Reputation: 89

/* 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); 
?>

Upvotes: 7

Vlatko Šurlan
Vlatko Šurlan

Reputation: 444

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

Upvotes: 4

Nick Phillips
Nick Phillips

Reputation: 96

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. The first place it tries to do this is the libreoffice directory in ~/.config ('~' means "the current user's home directory").

In Debian, by default, the www-data user has the home directory /var/www, and does not have write permission there.

If you make sure it has permission to either create ~/.config itself, or libreoffice within an existing ~/.config, I expect it will work.

Upvotes: 1

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

Upvotes: 7

Nijboer IT
Nijboer IT

Reputation: 1218

2 things, 1st the command is soffice --headless,
2nd i have an similar javaldx error and it has to do with permission.
when executing as root it works fine, but php executes as www-data.

if anybody knows a good way to execute libreoffice from php please let me know.
as i'm getting an error code 77 saying:

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

Upvotes: 0

Related Questions