在服务器上安装 LibreOffice 并配置中文支持※
本文档详细介绍了如何在 Linux 服务器(以 CentOS 和 Ubuntu 为例)上安装 LibreOffice,并配置中文字体和语言支持,使其能够在无图形界面的服务器上正常渲染和转换中文文档。
1. 安装 LibreOffice※
1.1 CentOS 系统※
1.1.1 更新系统软件包※
确保系统软件包是最新的。
sudo yum update -y
1.1.2 安装 LibreOffice 及依赖包※
通过 CentOS 的官方源直接安装 LibreOffice,无需手动下载源文件。
sudo yum install libreoffice -y
系统会自动解决依赖并安装 LibreOffice。
1.1.3 验证 LibreOffice 安装※
确认 LibreOffice 已成功安装:
libreoffice --version
1.2 Ubuntu 系统※
1.2.1 更新系统软件包※
确保系统软件包是最新的:
sudo apt update -y && sudo apt upgrade -y
1.2.2 安装 LibreOffice※
Ubuntu 自带 LibreOffice 的官方源,可以直接安装:
sudo apt install libreoffice -y
1.2.3 验证 LibreOffice 安装※
确认 LibreOffice 已成功安装:
libreoffice --version
2. 安装中文字体※
默认情况下,LibreOffice 可能无法正确显示中文字符。你需要手动安装中文字体,例如 Noto Sans CJK。
2.1 下载 Noto Sans CJK 字体※
从 Google Noto 字体项目下载适合的中文字体包:
cd /usr/share/fonts
sudo wget https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKsc-hinted.zip
2.2 解压字体文件※
sudo unzip NotoSansCJKsc-hinted.zip -d NotoSansCJK
2.3 更新字体缓存※
更新系统字体缓存,使新安装的字体生效:
sudo fc-cache -fv
2.4 删除无用的字体压缩包※
解压完成后,可以删除无用的 NotoSansCJKsc-hinted.zip
文件:
sudo rm -f /usr/share/fonts/NotoSansCJKsc-hinted.zip
2.5 验证字体是否安装成功※
fc-list | grep "Noto"
输出中应显示包含 "Noto Sans CJK" 的字体条目。
3. 可选:配置 LibreOffice 支持中文※
LibreOffice 的默认语言可能是英文,你可以将其设置为简体中文(可选)。
3.1 安装 LibreOffice 中文语言包※
CentOS 系统※
sudo yum install libreoffice-langpack-zh-Hans -y
Ubuntu 系统※
sudo apt install libreoffice-l10n-zh-cn -y
3.2 修改 LibreOffice 配置文件※
编辑 LibreOffice 配置文件,将默认语言设置为中文:
打开配置文件(若不存在则创建):
nano ~/.config/libreoffice/4/user/registrymodifications.xcu
添加以下配置内容,将默认语言设置为
zh-CN
:<?xml version="1.0" encoding="UTF-8"?> <oor:items xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <item oor:path="/org.openoffice.Setup/L10N"> <prop oor:name="ooLocale" oor:op="fuse"> <value>zh-CN</value> </prop> </item> </oor:items>
- 保存并退出。
4. 测试 LibreOffice 中文支持※
4.1 创建测试文件※
创建一个包含中文内容的测试文件:
echo "测试中文显示" > test.txt
4.2 使用 LibreOffice 转换文件※
使用 headless 模式 将文本文件转换为 PDF:
libreoffice --headless --convert-to pdf test.txt
4.3 验证输出※
检查输出目录,找到生成的 test.pdf
文件,确认其中的中文是否正常显示。
5. 常见问题及解决方法※
问题 1:字体缓存未更新※
解决方法:手动清理字体缓存并重新生成:
sudo rm -rf /var/cache/fontconfig/*
sudo fc-cache -fv
问题 2:中文显示为方块或乱码※
解决方法:确保 Noto Sans CJK 字体已正确安装,并重新生成字体缓存。
6. 总结※
通过以上步骤,你已成功在服务器上安装 LibreOffice,并完成了中文字体的配置。LibreOffice 现在可以通过 headless 模式 正确渲染和转换包含中文的文档。
命令速查表※
步骤 | CentOS 命令示例 | Ubuntu 命令示例 |
---|---|---|
安装 LibreOffice | sudo yum install libreoffice -y | sudo apt install libreoffice -y |
安装字体 | wget https://noto... && unzip ... | wget https://noto... && unzip ... |
更新字体缓存 | sudo fc-cache -fv | sudo fc-cache -fv |
删除压缩包 | sudo rm -f NotoSansCJKsc-hinted.zip | sudo rm -f NotoSansCJKsc-hinted.zip |
可选中文语言包 | sudo yum install libreoffice-langpack-zh-Hans -y | sudo apt install libreoffice-l10n-zh-cn -y |
文档转换 | libreoffice --headless --convert-to pdf test.txt | libreoffice --headless --convert-to pdf test.txt |