温馨提示×

CentOS下LibOffice字体问题

小樊
42
2025-11-20 04:39:10
栏目: 智能运维

CentOS 下 LibreOffice 字体问题排查与修复

一、快速自检

  • 查看系统是否缺少中文字体:执行命令:fc-list :lang=zh。若无输出或缺少常用中文字体(如 SimSun、FangSong、KaiTi、Microsoft YaHei),需安装字体。
  • 确认字体工具是否可用:执行 fc-match 仿宋fc-cache -v;若命令不存在,先安装:yum install -y fontconfig mkfontscale
  • 在 LibreOffice 中检查:打开 Writer → 工具 → 选项 → LibreOffice → 字体,查看“替换表”和“已安装字体”;若常用中文字体显示为“未找到/替换”,说明系统字体未被识别或未安装。

二、安装与注册常用中文字体

  • 推荐做法(系统级安装,所有用户与 LibreOffice 均可用):
    1. /usr/share/fonts/ 下新建目录并放入字体文件(TTF/OTF):
      • 示例:mkdir -p /usr/share/fonts/zhFont
      • 拷贝字体(示例从 Windows 拷贝,或从可信来源获取):cp /path/to/*.ttf /usr/share/fonts/zhFont/
    2. 生成索引并刷新缓存:
      • cd /usr/share/fonts/zhFont && mkfontscale && mkfontdir
      • fc-cache -fv
    3. 验证:fc-list :lang=zh | grep -i “simsun|fangsong|kaiti|yahei” 应能看到对应字体。
  • 用户级安装(仅当前用户可见,适合无 root 场景):
    • 将字体放入:~/.config/libreoffice/4/user/fonts/(如目录不存在请创建),然后重启 LibreOffice。
  • 常见可用字体:SimSun(宋体)FangSong(仿宋)KaiTi(楷体)Microsoft YaHei(微软雅黑)

三、解决 LibreOffice 转 PDF 或显示中的中文乱码

  • 若系统已安装字体但导出 PDF 仍乱码或“□/?”,多为字体未被实际选用或别名不匹配:
    1. 在 LibreOffice 中设置默认中文字体:工具 → 选项 → LibreOffice → 字体,将“替换表”中的 Andale Sans UI、Arial、Times New Roman 等常见西文字体映射到已安装的中文字体(如 SimSun、FangSong),并勾选“为西文/数字使用替换字体”。
    2. 使用字体别名(Fontconfig)修正匹配:
      • 新建:/etc/fonts/conf.d/10-fangsong-alias.conf
      • 内容示例:
        <?xml version="1.0"?>
        <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
        <fontconfig>
          <alias>
            <family>仿宋</family>
            <prefer>
              <family>FangSong</family>
            </prefer>
          </alias>
          <match target="pattern">
            <test qual="any" name="family"><string>FangSong</string></test>
            <edit name="family" mode="prepend">
              <string>Times New Roman</string>
            </edit>
          </match>
        </fontconfig>
        
      • 刷新缓存:fc-cache -fv;验证:fc-match 仿宋 应返回期望字体。
    3. 无头转换场景(如服务调用 soffice 转 PDF):确保服务环境也安装了相同字体并重建缓存;命令示例:soffice --headless --convert-to pdf input.docx --outdir /opt

四、常见坑位与修复

  • 命令拼写错误:应为 mkfontscalemkfontdir(不是“mkfontscaless/mkfontdirss”)。
  • 权限与目录:字体目录建议权限 755,字体文件 644;放在 /usr/share/fonts/ 下更便于系统全局识别。
  • 缓存未刷新:每次新增/替换字体后执行 fc-cache -fv
  • 依赖缺失:若提示与 fontconfig 相关命令不存在,安装:yum install -y fontconfig mkfontscale
  • 版本过旧:若系统为 CentOS 7(已于 2024-06-30 停止维护)且字体/兼容性问题频发,建议迁移至仍受支持的 RHEL/SLES,或使用 Flatpak/Snap 安装更新版本的 LibreOffice(可自动更新):
    • Flatpak:sudo yum install -y flatpak && flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo && flatpak install flathub org.libreoffice.LibreOffice
    • Snap:sudo snap install libreoffice --classic

0