安装LibreOffice
sudo yum update && sudo yum install libreoffice
(若需特定版本,可手动下载RPM包安装)
安装中文支持(避免乱码)
/usr/share/fonts/
。fc-cache -fv
刷新字体缓存。单文件转换
soffice --headless --convert-to pdf:writer_pdf_Export input.docx --outdir /output/path
--headless
:后台运行,无界面弹出。:writer_pdf_Export
:指定导出过滤器(不同格式需调整,如calc_pdf_Export
用于Excel转PDF)。批量转换
find /input/dir -name "*.docx" | while read file; do
soffice --headless --convert-to pdf "$file" --outdir /output/dir
done
或使用unoconv
(需先安装):
unoconv -f pdf /input/dir/*.docx
自定义输出路径与权限
--outdir
指定输出目录,确保目录存在且当前用户有写入权限。chmod
调整。服务未启动
若使用unoconv
或远程转换,需启动LibreOffice服务:
sudo systemctl start libreoffice.service
sudo systemctl enable libreoffice.service
中文乱码
fc-cache -fv
)。file
命令查看)。大文件转换失败
--norestore
参数避免加载上次会话(节省资源)。uno
库)实现程序化转换。soffice
的--accept
参数开启远程服务,配合unoconv
或自定义脚本实现跨服务器转换。参考来源: