Linux平台中使用PHP将word文档转pdf文档

一、背景

    在centos环境下,需要将word文档转换成pdf文档,由于未能查找到相关php扩展,因此借助linux的libreoffice实现转换。

二、具体实现

1、centos下安装libreoffice

yum install libreoffice

2、命令行执行word转pdf

   将 /home/file/word/test.docx转成pdf存放到 /home/file/pdf/ 目录下:

libreoffice --headless --convert-to pdf:writer_pdf_Export /home/file/word/test.docx --outdir  /home/file/pdf/

3、在php中执行的代码

<?php
$docfile = '/home/file/word/test.docx'; // word文件
$pdfdir = '/home/file/pdf/'; // pdf文件
$cmd = "export HOME=/tmp && libreoffice --headless -convert-to pdf {$docfile} -outdir {$pdfdir}";
$res = system($cmd); // exec($cmd);

三、可能碰到的问题

1、在centos执行"libreoffice --headless --convert-to pdf:writer_pdf_Export /home/file/word/test.docx --outdir  /home/file/pdf/"命令时,可能遇到:

root@snake:~/bin $ soffice --headless --convert-to txt:text blah.doc
/usr/lib64/libreoffice/program/soffice.bin X11 error: Can't open display:
   Set DISPLAY environment variable, use -display option
   or check permissions of your X-Server
   (See "man X" resp. "man xhost" for details)

此时需要安装libreoffice-headless,执行:

yum install libreoffice-headless

2、若转换出来的文件有乱码,则需添加中文字体,传送门>>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值