EmbeddedFont#embed crashes when postscriptName is a Uint8Array (Hermes / React Native)

未关闭
#1,687 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

  • #1753 来自 @pratik-desgn —— 已关闭,未合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
58/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
javascript, react-native
领域
mobile-dev

调研方向

检查 src/pdfkit 中报告的 built bundle 行附近的 EmbeddedFont#embed,并跟踪 postscriptName 在 PDF 生成过程中是如何使用的。在 Hermes 下使用名称为 Uint8Array 的字体复现该故障,然后验证字体嵌入完成且不会出现 replaceAll TypeError。

由索引模型根据 Issue 内容生成。

描述

In EmbeddedFont#embed (src/pdfkit, line ~32447 in the built bundle), the code assumes this.font.postscriptName is always a string:

const name = tag + '+' + this.font.postscriptName?.replaceAll(' ', '_');

However, fontkit's readString() returns a raw Uint8Array when TextDecoder doesn't support the requested encoding (e.g. x-mac-roman, utf-16be). This happens on Hermes (React Native), which only supports utf-8 and utf-16le.
This causes a TypeError: replaceAll is not a function at PDF generation time.

Suggested fix

const _psName = this.font.postscriptName;
const _psStr = typeof _psName === 'string'
  ? _psName
  : _psName instanceof Uint8Array
    ? Array.from(_psName, b => String.fromCharCode(b)).join('')
    : String(_psName || '');
const name = tag + '+' + _psStr.replace(/ /g, '_');

This handles all three cases (string, Uint8Array, null/undefined) and also replaces .replaceAll() with .replace(/ /g, '_') for broader engine compatibility.

Your environment

  • pdfkit: 0.17.2
  • pdfmake: 0.3.3
  • React Native: 0.81.4
  • Expo: 54.0.10
  • React: 19.1.0
  • JS Engine: Hermes
  • Node: 20.19.5
  • Platform: iOS / Android
主要语言
JavaScript
星标
10.7k
派生
1.2k
平均合并
21 小时 41 分钟
30 天内合并 PR
27

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

foliojs/pdfkit 的其他 Issue

查看 foliojs/pdfkit 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。