Search code examples
phpshelldocxlibreoffice

Libreoffice shell_exec failed inside PHP Script


I am having an Issue while running libreoffice from shell_exec inside a php script. This script is for converting DOCX to HTML via Libreoffice. The script is working well in terminal but not inside web browser. I have also tried to setup HOME to /tmp, but not working at all.

<?php

$soffice_path = '/usr/sbin/soffice';
$path_with_perm_777 = '/opt/lampp/htdocs/tmp'; 
$docx_file = __DIR__ . '/sample.docx';

echo `which soffice`;
echo `whoami`;
echo shell_exec('echo ${PATH}');

$command = escapeshellcmd('export HOME="/tmp"') . " && " . escapeshellcmd($soffice_path.' --headless --convert-to pdf --outdir '. $path_with_perm_777 .'/converted/ '.$docx_file);          

var_dump( shell_exec($command) );

Output in terminal :-

/usr/bin/soffice
jack
${PATH}:/usr/bin/custom/:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin
string(175) "convert /opt/lampp/htdocs/sample.docx -> /opt/lampp/htdocs/tmp/converted//sample.pdf using filter : writer_pdf_Export Overwriting: /opt/lampp/htdocs/tmp/converted//sample.pdf"

Output in web browser :-

/usr/sbin/soffice
jack
/usr/sbin:/usr/bin:/sbin:/bin:/root/bin
NULL

Output of ( ls -l /opt/lampp/htdocs/ ) :

drwxrwxrwx  2 jack jack 4096 Jan 23 13:50 tmp

System Info:

PHP 5.6.39, Manjaro Linux 18.0.2, LibreOffice 6.1.4.2


Solution

  • Finally, I fixed it. After Inspecting /opt/lampp/logs/error_log, I got these three lines :-

    /usr/lib/libreoffice/program/oosplash: /opt/lampp/lib/libstdc++.so.6: version 'GLIBCXX_3.4.18' not found (required by /usr/lib/libreoffice/program/libuno_sal.so.3)

    /usr/lib/libreoffice/program/oosplash: /opt/lampp/lib/libstdc++.so.6: version 'GLIBCXX_3.4.11' not found (required by /usr/lib/libreoffice/program/libuno_sal.so.3)

    /usr/lib/libreoffice/program/oosplash: /opt/lampp/lib/libstdc++.so.6: version 'CXXABI_1.3.9' not found (required by /usr/lib/libreoffice/program/libuno_sal.so.3)

    LibreOffice could not find required libraries because of missing LD_LIBRARY_PATH variable. After setting up LD_LIBRARY_PATH, LibreOffice successfully converted DOCX Document to PDF File.

    $command = escapeshellcmd('export LD_LIBRARY_PATH="/usr/lib:/usr/lib32:/usr/local/lib"') . ' && ' . escapeshellcmd('export HOME="/tmp"') . ' && ' . escapeshellcmd($soffice_path.' --headless --convert-to pdf --outdir '. $path_with_perm_777 .'/converted '.$docx_file);
    var_dump( shell_exec($command) );
    

    Output in Web Browser:-

    string(175) "convert /opt/lampp/htdocs/sample.docx -> /opt/lampp/htdocs/tmp/converted/sample.pdf using filter : writer_pdf_Export Overwriting: /opt/lampp/htdocs/tmp/converted//sample.pdf "