You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReportLab无法注册NotoColorEmoji.ttf,提示缺少位置表

问题:ReportLab导入NotoColorEmoji.ttf时报错"missing location table"

问题场景

我使用ReportLab工具,尝试导入个人目录/home/peterr/.local/share/fonts/下的NotoColorEmoji.ttf字体,代码如下:

fontpath = '/home/peterr/.local/share/fonts/'
#fontpath = '/usr/share/fonts/truetype/noto/' # also here
ttfFile = os.path.join(fontpath, 'NotoColorEmoji.ttf')
pdfmetrics.registerFont(TTFont("tofu", ttfFile))

报错信息

执行pdfmetrics.registerFont(TTFont("tofu", ttfFile))时触发以下错误:

Traceback (most recent call last):
  File "/usr/lib/python3.12/idlelib/run.py", line 580, in runcode
    exec(code, self.locals)
  File "<pyshell#103>", line 1, in <module>
  File "/home/peterr/PVE/GPE/lib/python3.12/site-packages/reportlab/pdfbase/ttfonts.py", line 1207, in __init__
    self.face = TTFontFace(filename, validate=validate, subfontIndex=subfontIndex)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/peterr/PVE/GPE/lib/python3.12/site-packages/reportlab/pdfbase/ttfonts.py", line 1088, in __init__
    TTFontFile.__init__(self, filename, validate=validate, subfontIndex=subfontIndex)
  File "/home/peterr/PVE/GPE/lib/python3.12/site-packages/reportlab/pdfbase/ttfonts.py", line 448, in __init__
    self.extractInfo(charInfo)
  File "/home/peterr/PVE/GPE/lib/python3.12/site-packages/reportlab/pdfbase/ttfonts.py", line 869, in extractInfo
    if 'loca' not in self.table: raise TTFError('missing location table')
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
reportlab.pdfbase.ttfonts.TTFError: missing location table

疑问

相同操作流程可以正常导入该目录下的部分中文字体,想问是不是NotoColorEmoji.ttf文件损坏?求解决建议。


分析与解决建议

  • 不是文件损坏。NotoColorEmoji.ttf是彩色emoji字体,采用COLR/CPAL等彩色字体扩展格式,而ReportLab的TTFont模块仅支持传统单色TrueType字体,不兼容这种带彩色表的特殊TTF结构,所以会因缺少传统TTF必需的loca表报错。
  • 可行的解决方向:
    1. 替换为单色emoji字体:使用标准TTF格式的单色版本,比如NotoEmoji-Regular.ttf,这类字体没有彩色扩展,能被ReportLab正常识别。可以检查系统字体目录(比如/usr/share/fonts/truetype/noto/)是否有该字体,替换路径后重试。
    2. 转图片插入彩色emoji:如果必须保留彩色效果,先用PIL/Pillow库将emoji渲染为图片,再通过ReportLab的Image类将图片插入PDF文档。
    3. 跳过ReportLab直接导入:如果使用的是ReportLab的PLATYPUS框架,也可以尝试用第三方库处理彩色emoji后再嵌入,但复杂度较高,优先推荐前两种方案。

内容的提问来源于stack exchange,提问作者Pedroski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:50:10