AWS Lambda 上的破损字体
This article was translated from English: Does it need improvement?
Translated
View the article in English
在 Linux 中渲染 PDF 文档可能会导致字符损坏,如下图所示。 有时字符在 Windows 环境中显示正常,但在 Linux 中却不正常。

原因
原因是操作系统缺少该字体。
解决方案
如果您已经知道字体名称,那就很好——您可以跳过步骤 1。
- 要找出字体名称,请尝试在 Windows 上渲染 HTML,并使用 Adobe 检查字符。
- 一旦确定了字体名称,您可以在线下载字体文件,例如从Google Fonts下载。
- 要通过 Dockerfile 添加和安装字体,请将字体放入项目中,并将 '复制到输出目录' 设置为 '始终复制'。接下来,使用以下代码在 Dockerfile 中添加并安装它:
# Copy the Cambria Math font into the container
# Ensure the font file (cambria.ttc) exists in your project directory
COPY cambria.ttc /usr/share/fonts/
# Install fontconfig to update the font cache
# This will install fontconfig and refresh the font cache to recognize the newly added fonts
RUN dnf install -y fontconfig && \
fc-cache -fv
解释
-
COPY cambria.ttc /usr/share/fonts/:此行将 Cambria Math 字体从您的项目目录复制到容器的字体目录中。 请确保字体文件(
cambria.ttc)存在于项目目录中。 -
RUN dnf install -y fontconfig && \ : 此命令安装
fontconfig软件包,该软件包是管理和配置 Linux 中字体访问所必需的。-y标志会自动确认软件包的安装。 - fc-cache -fv:此命令更新字体缓存,确保新复制的字体在容器内的应用程序中可用。 标志
-f(force) 和-v(verbose) 分别用于强制重新生成缓存和提供详细输出。
准备开始了吗?
Nuget 下载 18,560,885 | 版本: 2026.4 刚刚发布

