Skip to content

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

@MikkelVibe

Description

@MikkelVibe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions