在 CentOS 上,LibreOffice 可以在无界面(headless)模式下进行文档格式转换,常用于把 Word、Excel、PPT 等转成 PDF 或其他格式。
下面按常见场景说明。
yum install -y libreoffice-headless libreoffice-writer libreoffice-calc libreoffice-impress
dnf install -y libreoffice-headless libreoffice-writer libreoffice-calc libreoffice-impress
安装完成后验证:
libreoffice --version
# 或
soffice --version
libreoffice --headless --convert-to pdf test.docx
或指定输出目录:
libreoffice --headless --convert-to pdf --outdir /data/pdf test.docx
libreoffice --headless --convert-to pdf:.writer_pdf_Export doc.docx
libreoffice --headless --convert-to pdf xls.xlsx
libreoffice --headless --convert-to pdf ppt.pptx
libreoffice --headless --convert-to html test.docx
libreoffice --headless --convert-to txt test.docx
libreoffice --headless --convert-to csv test.xlsx
for f in *.docx; do
libreoffice --headless --convert-to pdf "$f"
done
通常是 用户目录冲突(root 下容易出问题)
✅ 解决:
libreoffice --headless --convert-to pdf \
-env:UserInstallation=file:///tmp/lo_profile test.docx
或统一用普通用户执行。
安装中文字体:
yum install -y wqy-zenhei-fonts
有些版本只有:
soffice --headless --convert-to pdf test.docx
#!/bin/bash
INPUT=$1
OUTPUT_DIR=/data/pdf
libreoffice --headless \
-env:UserInstallation=file:///tmp/lo_$RANDOM \
--convert-to pdf --outdir $OUTPUT_DIR $INPUT
如果你有 具体转换需求(如 docx→pdf、批量、Java/PHP 调用、Docker 环境),可以告诉我,我给你更精确的方案。