PHP使用LibreOffice实现word转pdf


LibreOffice是OpenOffice办公套件衍生版, 同样免费开源,并且是跨平台的,支持windows和linux服务器安装。

php调用命令实现文档转换:

$retval = 1;
// exec() might be disabled
$cmd = 'D:/soft/LibreOfficePortablePrevious/LibreOfficeWriterPortable.exe  --headless --convert-to pdf 1.doc --outdir ./';
if (function_exists('exec')){
    @exec($cmd, $output, $retval);
}
// Did it work?
if ($retval > 0){
    exit('process_failed');
}
echo 'success';

Centos下的安装及使用

直接使用yum安装即可:

  1. yum install libreoffice
  2. yum install libreoffice-headless  //该工具用来启动Libreoffice

2、执行命令转换pdf文档

跟windows类似,执行以下命令即可转换文档:

soffice --headless --convert-to pdf {文档路径} --outdir {导出目录路径}

 

3.解决中文不显示,变方框的问题

在linux下转换pdf,可能会出现中文字体不被识别,变成小方框的问题,解决步骤:
1.把Windows下的字体C:\Windows\Fonts下的常用字体,即simsun.ttc等复制到usr/share/fonts目录下。
2 修改字体权限

sudo chmod 644 simsun.ttc

3. 更新字体缓存:

sudo fc-cache -fv

 

 

【杀死子进程命令】
ps -ef|grep soffice | awk '{print $2}' | xargs kill -9
 
【重启命令】
/usr/lib64/libreoffice/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

 

 【重启成功截图】

 

 

 

参考:http://www.884358.com/php-libreoffice-word-pdf/

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM