在 CentOS 上用 LibreOffice(libreoffice) 转换文档,常见做法是通过 命令行(headless 模式)。下面按常见场景给你说明。
yum install -y libreoffice
dnf install -y libreoffice
如需中文支持:
dnf install -y libreoffice-langpack-zh-CN
验证:
libreoffice --version
✅ 推荐方式:使用 --headless 模式(无界面)
libreoffice --headless --convert-to pdf test.docx
指定输出目录:
libreoffice --headless --convert-to pdf --outdir /tmp test.docx
libreoffice --headless --convert-to pdf *.docx
| 目标格式 | 命令 |
|---|---|
| docx → doc | libreoffice --headless --convert-to doc test.docx |
| xlsx → csv | libreoffice --headless --convert-to csv test.xlsx |
| pptx → pdf | libreoffice --headless --convert-to pdf test.pptx |
libreoffice --headless --convert-to html test.docx
libreoffice --headless --convert-to txt test.docx
原因:缺少中文字体
解决:
dnf install -y wqy-microhei-fonts wqy-zenhei-fonts
或复制 Windows 字体到:
/usr/share/fonts/
fc-cache -fv
尝试指定 soffice 路径:
/usr/bin/soffice --headless --convert-to pdf test.docx
LibreOffice 默认不允许 root 直接运行:
✅ 解决方式:
libreoffice --headless --convert-to pdf test.docx --norestore -env:UserInstallation=file:///tmp/libreoffice-root
#!/bin/bash
libreoffice --headless --norestore --convert-to pdf "$1"
| 工具 | 适用 |
|---|---|
pandoc |
Markdown / docx / html |
unoconv |
依赖 LibreOffice |
docx2pdf(Python) |
Windows / macOS |
✅ CentOS + LibreOffice 转 PDF 最简命令
libreoffice --headless --convert-to pdf test.docx
如果你有 具体转换需求(例如:
可以直接告诉我,我给你定制命令或脚本。