centos下php unoconv转换word失败及解决

作者:stoat 发布时间:2018-08-11 分类:技术

在shell模式下的root用户时,centos下使用php实现word转html并读取导入数据库能够正常将word转换为其他格式的文件。

打印unoconv日志,代码:

shell_exec('UNO_PATH=/opt/libreoffice5.4/program unoconv -vvv -f pdf  123.doc 2>&1 | tee /www/wwwroot/www.school2.test/unoconv.log');

报错如下:
Verbosity set to level 3
DEBUG: Connection type: socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
DEBUG: Existing listener not found.
DEBUG: Launching our own listener using /opt/libreoffice5.4/program/soffice.bin.
DEBUG: Process /opt/libreoffice5.4/program/soffice.bin (pid=3688) exited with 77.
Error: Unable to connect or start own listener. Aborting.
Using office base path: /opt/libreoffice5.4
Using office binary path: /opt/libreoffice5.4/program
LibreOffice listener successfully started. (pid=3688)

主原因:权限问题导致无法生成,web模式下php-fpm的执行者为www用户,而命令行模式时执行者为root用户。

解决方案
1,修改www用户的主目录,修改nginx下的www用户的权限

ps -ef | grep php

chmod u+w /etc/sudoers
vi /etc/sudoers
新增
www    ALL=(ALL)       ALL
www        ALL=(ALL)       NOPASSWD: ALL

chmod u-w /etc/sudoers

chown www /www
设置/www目录为用户主目录,并设置读写权限

重启php服务

采用该方案后依旧不成功,改为方案2。

2,修改php中的命令,测试成功

shell_exec('UNO_PATH=/opt/libreoffice5.4/program sudo -E -u root  unoconv -f html -o 123.html 123.doc');

3,采用守护程序

[program:libreoffice-listener]
command=/usr/lib/libreoffice/program/soffice.bin --headless --accept="socket,host=127.0.0.1,port=2002;urp;" --nofirststartwizard
user=root
autostart=true
autorestart=true
stdout_logfile=/var/log/libreoffice.log

http://blog.stoatme.com/342.html

参考链接:
https://github.com/dagwieers/unoconv/issues/359
https://github.com/dagwieers/unoconv/issues/87#issuecomment-18800070%29
https://tech.rgou.net/php/converting-documents-odt-doc-to-pdf-on-php-with-unoconv-libreoffice/
https://www.bountysource.com/issues/38356600-process-usr-lib-libreoffice-program-soffice-bin-pid-7091-exited-with-77

原文地址:centos下php unoconv转换word失败及解决 by 雪鼬博客

标签:none

评论已关闭