liunx命令word文档和excel表格转换成pdf

发布时间 2024-01-11 11:37:26作者: 张小爽

1、  Anolis OS-8.x和centos7.x系统安装libreoffice

#直接使用yum安装
yum install -y libreoffice

#验证

libreoffice --version

LibreOffice 7.1.8.1 10(Build:1)

#后台启动libreoffice服务

nohup libreoffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &

#检查服务是否正常

ps -ef | grep libreoffice

root        4067    1015  0 10:06 pts/0    00:00:00 /usr/lib64/libreoffice/program/oosplash --headless --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard

root        4096    4067  1 10:06 pts/0    00:00:00 /usr/lib64/libreoffice/program/soffice.bin --headless --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard

root        4102    1015  0 10:06 pts/0    00:00:00 grep --color=auto libreoffice

#查看端口号

netstat -ntlup | grep 8100

tcp        0      0 127.0.0.1:8100          0.0.0.0:*               LISTEN      4096/soffice.bin

 

2、其他系统没有yum源直接下载rpm包进行安装

下载地址:
https://zh-cn.libreoffice.org/download/libreoffice/
wget https://download.documentfoundation.org/libreoffice/stable/7.6.4/rpm/x86_64/LibreOffice_7.6.4_Linux_x86-64_rpm.tar.gz

#解压并安装

tar xf LibreOffice_7.6.4.1_Linux_x86-64_rpm.tar.gz

cd LibreOffice_7.6.4.1_Linux_x86-64_rpm/RPMS/

yum -y install *.rpm

#需要把版本号去掉,代码直接调用的libreoffice这个命令

mv  /usr/bin/libreoffice7.6 /usr/bin/libreoffice

#验证命令

libreoffice --version

#rpm包安装直接启动会报错,需要安装依赖

yum install libXinerama cairo -y

#后台启动服务

nohup libreoffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &

#检查服务是否正常

ps -ef | grep libreoffice

root        4067    1015  0 10:06 pts/0    00:00:00 /usr/lib64/libreoffice/program/oosplash --headless --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard

root        4096    4067  1 10:06 pts/0    00:00:00 /usr/lib64/libreoffice/program/soffice.bin --headless --accept=socket,host=127.0.0.1,port=8100;urp; --nofirststartwizard

root        4102    1015  0 10:06 pts/0    00:00:00 grep --color=auto libreoffice

#查看端口号

netstat -ntlup | grep 8100

tcp        0      0 127.0.0.1:8100          0.0.0.0:*               LISTEN      4096/soffice.bin

 

3、将Windows字体拷贝到liunx服务器上防止乱码

# 将Windows字体(C:\Windows\Fonts)拷贝到

cd /usr/share/fonts

#安装依赖

yum install -y fontconfig mkfontscale

#执行两条命令

mkfontdir

fc-cache -fv

# 命令执行成功后终端最后一行会显示 fc-cache: succeeded

#验证

fc-list :lang=zh

 

手动将word文档转换成pdf文档

libreoffice --headless --invisible --convert-to pdf ./input.docx --outdir ./