温馨提示×

LibOffice在CentOS上的字体安装与管理

小樊
37
2025-12-13 19:50:13
栏目: 智能运维

适用说明与准备

  • 本文以 LibreOffice(常被误写为“LibOffice”)在 CentOS 7/8 上的字体安装与管理为核心,涵盖系统级与用户级安装、缓存刷新、优先级与替换、验证与排错,以及无头转换场景的注意事项。
  • 准备要点:
    • 安装字体工具:sudo yum install -y fontconfig
    • 常见字体目录:/usr/share/fonts/usr/local/share/fonts、用户目录 ~/.local/share/fonts
    • 刷新字体缓存:fc-cache -f -v
    • 查看字体与语言:fc-list :lang=zhfc-match “字体名”
    • 如无图形界面,可用命令行完成全部操作。

系统级与用户级安装步骤

  • 系统级安装(所有用户可用)
    1. 拷贝字体到系统目录(TTF/OTF/TTC 等)
      • 示例:sudo cp /path/to/*.ttf /usr/share/fonts/
    2. 刷新缓存:sudo fc-cache -f -v
    3. 验证:fc-list :lang=zh | grep “字体名”
  • 用户级安装(仅当前用户)
    1. 创建用户字体目录:mkdir -p ~/.local/share/fonts
    2. 拷贝字体并刷新:fc-cache -f -v
    3. 验证:fc-list :lang=zh | grep “字体名”
  • 无 root 权限时的替代
    • 将字体放入用户目录(见上),或在 ~/.config/fontconfig/fonts.conf 中配置别名/替换/优先级,然后刷新缓存。

字体配置与优先级管理

  • 使用 FontConfig 配置别名、替换与优先级
    • 全局:/etc/fonts/local.conf
    • 用户:~/.config/fontconfig/fonts.conf
    • 示例(将 serif 家族优先映射到“Noto Serif CJK SC”,可按需替换为你的字体):
      <?xml version="1.0"?>
      <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
      <fontconfig>
        <alias>
          <family>serif</family>
          <prefer>
            <family>Your Font Name</family>
          </prefer>
        </alias>
      </fontconfig>
      
    • 使配置生效:fc-cache -f -v
  • 常用命令
    • 列出中文字体:fc-list :lang=zh
    • 精确匹配字体:fc-match “Arial”
    • 查看替换与家族:fc-match -s “sans-serif”
    • 说明:LibreOffice 直接使用 FontConfig 提供的字体列表与替换规则,无需单独在应用内注册。

验证与常见问题排查

  • 验证 LibreOffice 是否识别
    • 命令行检查字体:fc-list :lang=zh | grep “字体名”
    • 在 LibreOffice Writer 中:工具 → 选项 → 字体,查看可用字体;或打开文档检查渲染效果。
  • 无头转换(headless)中文乱码
    • 原因多为缺少中文字体或未刷新缓存。
    • 解决步骤:
      1. 拷贝中文字体(如 simsun.ttc)到 /usr/share/fonts/(或用户目录)
      2. 刷新缓存:fc-cache -f -v
      3. 转换命令示例:libreoffice --headless --convert-to pdf input.docx --outdir /path/to/output
    • 若仍异常,检查是否安装了常用字体包:sudo yum install -y ttf-dejavu ttf-liberation wqy-zenhei wqy-microhei wqy-bitmapfont 并再次刷新缓存。

常用字体包与一键命令清单

  • 安装常用字体包(解决缺字/替换降级)
    • sudo yum install -y ttf-dejavu ttf-liberation wqy-zenhei wqy-microhei wqy-bitmapfont
  • 一键复制 Windows 字体并生效(需 root)
    • sudo cp -r /path/to/windows/Fonts/ /usr/share/fonts/*
    • sudo chmod -R 755 /usr/share/fonts/
    • sudo fc-cache -f -v
  • 无头转换 PDF
    • libreoffice --headless --convert-to pdf input.docx --outdir /path/to/output
  • 提示
    • 字体目录与缓存操作建议使用 sudo;用户级安装无需 sudo。
    • 修改 /etc/fonts/fonts.conf~/.config/fontconfig/fonts.conf 后务必执行 fc-cache -f -v

0