软件安装
unoconv下载解压:https://gitee.com/mirrors/dagwieers-unoconv?_from=gitee_search
libreoffice下载安装:https://www.libreoffice.org/download/download/
unoconv可以在多种格式中转换,查看可以转换的格式
G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe unoconv --show
其余参数可以通过以下命令查询
G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe unoconv -h
windwos环境
配置环境变量UNO_PATH 环境变量的值值配置到Liboffice的program目录下
使用liboffice自带的python环境G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe,避免不兼容情况
测试
在unoconv的目录下执行,将文件file.docx转换成pdf
G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe unoconv -f pdf -o file.pdf file.docx
php代码
// 设置LibreOffice的环境变量,否则shell_exec执行的时候找不到可用的office组件将无法转换
putenv('UNO_PATH=G:\LibreOffice_6.3.2\program');
$file = 'H:/out/filename'; // 要使用绝对路径
shell_exec('G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe C:\Users\Administrator\Desktop\unoconv-0.9\unoconv -f pdf -o ' . $file . '.pdf' . ' ' . $file . '.docx');
# 可以打印详细的日志以做调试
var_dump(shell_exec('G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe C:\Users\Administrator\Desktop\unoconv-0.9\unoconv -vvv -f pdf -o ' . $file . '.pdf' . ' ' . $file . '.docx'));
Linux环境
转载白俊遥博客内容地址:https://baijunyao.com/article/145
加速转换
以指定端口启动unoconv监听器,端口默认为2002;当unoconv执行转换时会寻找已启动的监听器,如果没有已启动的监听器,会启动临时监听器,这样会消耗时间
G:\LibreOffice_6.3.2\program\python-core-3.5.7\bin\python.exe unoconv -l -p 2002