它的路径是:/Applications/LibreOffice.app/Contents/MacOS/soffice
php调用具体转换命令为:
<?php
switch(strtolower(PHP_OS)){
case 'darwin':
$libreoffice = '/Applications/LibreOffice.app/Contents/MacOS/soffice';
break;
case 'linux':
$libreoffice = 'sudo /usr/bin/libreoffice';
break;
case 'windows':
$libreoffice = 'C:\Program Files\LibreOffice 5\program\soffice.exe';
break;
default:
$libreoffice = 'sudo /usr/bin/libreoffice';
} $inputDir = WEB_ROOT . '/web' . $pdfupload_dir;
$outputFile = WEB_ROOT . '/web' . $node_ppt_file['uri'] . $node_ppt_file['filename'];
$cmd = "{$libreoffice} --headless --convert-to pdf --outdir {$inputDir} {$outputFile} 2>&1"; exec($cmd);
//exec命令的第二、三个参数可以输出具体报错信息(当然这个报错是结合命令末尾的:2>&1)
//$res = exec($cmd,$output,$return_var);
//print_r($output);
//print_r($return_var);
//exit;
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir /Users/Bruce/www/tp_center/frontend/web/uploads/casepdf/20171204/ /Users/Bruce/www/tp_center/frontend/web/uploads/caseppt/20171204/50112bdd50d6a36e87b5b91a9ffe6821.pptx 2>&1
解决方法:
I had the same problem on MacOS High Sierra, PHP 7.0 installed from Homebrew. The problem is that FPM $PATH
contains (as echoed by <php echo getenv('PATH'); ?>
) /usr/bin:/bin:/usr/sbin:/sbin
, which doesn't include Brew's /usr/local/bin/
install path.
To solve I added
env[PATH] = /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
to /usr/local/etc/php/7.0/php-fpm.d/www.conf
, restarted php70 and worked!
6、对于CentOS,命令前一定要加上sudo,否则肯定会出现权限问题
且需要配置sudo权限,配置方法如下:
PHP脚本调用exec执行权限设置
1.PHP 调用exec执行linux 系统命令时,是以apache 默认user执行,user设置在 httpd.conf文件
User ×××××
Group ××××
2.如果直接调用exec ('****') 执行root级命令,会报权限不足,这时需要用 sudo exec ('')
3.sudo 执行过程需要输入用户密码,势必对PHP执行带来极大不便,如何取消密码、屏蔽过程信息等,可以在 sudo 配置文件/etc/sudoers里设置
添加sudo用户
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
apache ALL=(ALL) NOPASSWD: ALL
注释掉 Defaults requiretty (屏蔽中途信息)
添加PATH: Defaults secure_path = /sbin:/bin:/usr/sbin
4.一些可执行脚本需要调用lib,默认不会继承root的LD_LIBRARY_PATH ,报错如下:
error while loading shared libraries: libavformat.so.56: cannot open shared object file: No such file or directory
解决办法:编辑 /etc/lib.so.conf
添加相应lib路径:/usr/ffmpeg/lib/
执行sudo ldconfi
以上是我遇到的坑,看到这篇文章的童鞋,希望你们能跳过这些坑。