Libreoffice shell_exec在PHP脚本内

时间:2019-01-23 09:12:55

标签: php shell docx libreoffice

在PHP脚本中从shell_exec运行libreoffice时出现问题。该脚本用于通过Libreoffice将DOCX转换为HTML。该脚本在终端中运行良好,但在Web浏览器中却无法正常运行。我还尝试将HOME设置为/ tmp,但根本无法正常工作。

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

端子输出:-

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

在网络浏览器中输出:-

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

({ls -l /opt/lampp/htdocs/)的输出:

drwxrwxrwx  2 jack jack 4096 Jan 23 13:50 tmp

系统信息:

  

PHP 5.6.39,Manjaro Linux 18.0.2,LibreOffice 6.1.4.2

1 个答案:

答案 0 :(得分:1)

最后,我修复了它。检查完/ opt / lampp / logs / error_log之后,我得到了以下三行:-

  

/ usr / lib / libreoffice / program / oosplash:/opt/lampp/lib/libstdc++.so.6:   找不到版本“ GLIBCXX_3.4.18”(由   /usr/lib/libreoffice/program/libuno_sal.so.3)

     

/ usr / lib / libreoffice / program / oosplash:/opt/lampp/lib/libstdc++.so.6:   找不到版本“ GLIBCXX_3.4.11”(由   /usr/lib/libreoffice/program/libuno_sal.so.3)

     

/ usr / lib / libreoffice / program / oosplash:/opt/lampp/lib/libstdc++.so.6:   找不到版本“ CXXABI_1.3.9”(由   /usr/lib/libreoffice/program/libuno_sal.so.3)

由于缺少LD_LIBRARY_PATH变量,LibreOffice无法找到所需的库。设置LD_LIBRARY_PATH后,LibreOffice成功将DOCX文档转换为PDF文件。

$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) );

Web浏览器中的输出:-

  

string(175)“转换/opt/lampp/htdocs/sample.docx->   /opt/lampp/htdocs/tmp/converted/sample.pdf使用过滤器:   writer_pdf_导出覆盖:   /opt/lampp/htdocs/tmp/converted//sample.pdf“