软件:https://www.libreoffice.org/
安装完后,创建软连接: ln -s path/to/soffice /usr/local/bin/soffice
转换命令:
1 |
/usr/local/bin/soffice --headless --convert-to pdf ["path/to/source.doc(x)"] --outdir "/tmp" |
PS,如果报错,尝试以下步骤:
1.安装需要的扩展, yum install libXinerama.x86_64 (可以先 yum search libXinerama 看有哪些)
【参考:https://superuser.com/questions/688871/error-while-loading-shared-libraries-libxinerama-so-1-cannot-open-shared-objec】
2.安装java环境:
1 2 3 |
yum install java-1.8.0-openjdk-devel yum install java-1.8.0-openjdk |
【参考:https://phoenixnap.com/kb/install-java-on-centos】
3.若执行转换命令发现有中文乱码,安装字体,并重启机器:
1 |
yum groupinstall "fonts" |
将 /etc/locale.conf 内容改为 LANG="zh_CN.UTF-8" ,然后reboot重启。
【参考:https://blog.csdn.net/xujingcheng123/article/details/84643021】
附php参考代码:(带超时设置)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * 将文件转换成pdf格式 * @param $source * @param $desti * @param string $bin * @throws * @return string */ public static function convert2Pdf($source, $desti, $bin = "/usr/local/bin/soffice"){ @exec("timeout 3 {$bin} --headless --convert-to pdf --outdir {$desti} {$source} > /dev/null"); $pathInfo = pathinfo($source); $destiFilePath = $desti."/{$pathInfo['filename']}.pdf"; if(!file_exists($destiFilePath)) throw new Exception("转换文件格式失败, ".__CLASS__."::".__FUNCTION__.", source:{$source}"); return $destiFilePath; } |