php CentOS7下通过OpenOffice+jodconverter实现将Excel转PDF

一、安装openoffice

1.下载openoffice

openoffice官网 http://www.openoffice.org/download/index.html

wget -c 资源地址  #使用 wget -c 断点续传
2.解压openoffice
3.安装
CD zh-CN/RPMS/
yum localinstall *.rpm
4.再安装(我觉得不装这个也行,未测试)
CD desktop-integration/
yum localinstall openoffice4.1.7-redhat-menus-4.1.7-9800.noarch.rpm
5.启动
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
6.查看是否成功
netstat -lnp | grep 8100

显示:
tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 8987/soffice.bin
成功!

二、安装java运行环境

查看yum库中都有哪些jdk版本,运行:

yum search java|grep jdk

选择版本进行安装:

yum install java-11-openjdk.x86_64

查看是否安装成功:

java -version

显示:

openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode, sharing)

三、下载JODConverter(Java OpenDocument Converter)

JODConverter下载地址:https://sourceforge.net/projects/jodconverter

四、测试转换

test.xls=>test.pdf

cd jodconverter-2.2.2/lib
java -jar jodconverter-cli-2.2.2.jar test.xls test.pdf

五、解决中文不显示问题

1.下载字体

simhei.ttf 黑体
simsun.ttc 宋体
两种字体文件
放到如下目录
/opt/openoffice4/share/fonts/truetype

2.更新字体缓存
fc-cache
3.重启openoffice
ps ax|grep soffice 

显示如下:

22739 pts/5 S 0:00 /bin/sh /opt/openoffice.org3/program/soffice -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard 
22747 pts/5 Sl 0:01 /opt/openoffice.org3/program/soffice.bin -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard 
23789 pts/5 S+ 0:00 grep soffice 

关闭soffice进程

kill 22739 

再次启动openoffice

nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

六、php调用

<?php
$command = "java -jar jodconverter-cli-2.2.2.jar test.xls test.pdf";
//exec($command, $output);
//system($command);
passthru($command);
发布了21 篇原创文章 · 获赞 2 · 访问量 1664

猜你喜欢

转载自blog.csdn.net/qq_23062949/article/details/103735602
今日推荐