在 CentOS 上使用 LibreOffice 将文档(如 docx/xlsx/pptx)转换为 PDF 失败,常见原因和解决办法如下:
LibreOffice 在服务器上常缺少中文字体,导致转换失败或 PDF 无内容。
解决:
yum install -y wqy-zenhei-fonts wqy-microhei-fonts
或手动复制 Windows 字体到:
/usr/share/fonts/
fc-cache -fv
必须使用 headless 模式。
正确命令:
libreoffice --headless --convert-to pdf file.docx
或:
soffice --headless --convert-to pdf file.docx
很多 CentOS 只装了 libreoffice-core,缺组件。
解决:
yum groupinstall "Office Suite and Productivity"
或
yum install libreoffice-writer libreoffice-calc libreoffice-impress
LibreOffice 不支持并发。
解决:
Web 服务调用时常见。
检查:
ps aux | grep soffice
确保运行用户有:
某些格式依赖 Java。
yum install java-1.8.0-openjdk
尝试用 LibreOffice 手动打开验证。
getenforce
如为 Enforcing,可临时测试:
setenforce 0
soffice --headless --nofirststartwizard \
--convert-to pdf --outdir /tmp/out /data/test.docx
查看详细错误:
soffice --headless --convert-to pdf test.docx 2>&1
如果你能提供:
我可以更精确帮你定位。