parallax/jsPDF · error · Error

Invalid PDF Name Object: " + str + ", Only accept ASCII char

Error message

Invalid PDF Name Object: " + str + ", Only accept ASCII characters.

What it means

Thrown by toPDFName when the input string contains any character above U+00FF, i.e. outside Latin-1. PDF Name Objects per the PDF 1.3 spec are byte-oriented, so jsPDF rejects code points it cannot represent in a single byte. (The inline comment says 'non ascii' but the regex /[^-ÿ]/ actually permits the full Latin-1 range and only blocks code points > 0xFF such as CJK, Cyrillic, or emoji.)

Source

Thrown at src/libs/pdfname.js:10

/**
 * Convert string to `PDF Name Object`.
 * Detail: PDF Reference 1.3 - Chapter 3.2.4 Name Object
 * @param str
 */
function toPDFName(str) {
  // eslint-disable-next-line no-control-regex
  if (/[^\u0000-\u00ff]/.test(str)) {
    // non ascii string
    throw new Error(
      "Invalid PDF Name Object: " + str + ", Only accept ASCII characters."
    );
  }
  var result = "",
    strLength = str.length;
  for (var i = 0; i < strLength; i++) {
    var charCode = str.charCodeAt(i);
    if (
      charCode < 0x21 ||
      charCode === 0x23 /* # */ ||
      charCode === 0x25 /* % */ ||
      charCode === 0x28 /* ( */ ||
      charCode === 0x29 /* ) */ ||
      charCode === 0x2f /* / */ ||
      charCode === 0x3c /* < */ ||
      charCode === 0x3e /* > */ ||
      charCode === 0x5b /* [ */ ||
      charCode === 0x5d /* ] */ ||

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Strip or transliterate characters above U+00FF from the font/name string before it reaches jsPDF.
  2. Keep font PostScript names ASCII-only when generating/converting the font (recommended PDF practice anyway).
  3. If you must keep the characters, encode the name yourself as #-escaped Latin-1 bytes instead of relying on toPDFName for the raw string.

Example fix

// before
out("/BaseFont /" + toPDFName(font.postScriptName)); // postScriptName has CJK

// after
var safeName = font.postScriptName.replace(/[^\u0000-\u00ff]/g, '');
out("/BaseFont /" + toPDFName(safeName));
Defensive patterns

Strategy: validation

Validate before calling

function isLatin1(str) {
  return typeof str === 'string' && !/[^\u0000-\u00ff]/.test(str);
}
if (!isLatin1(name)) {
  name = name.replace(/[^\u0000-\u00ff]/g, ''); // or transliterate
}
toPDFName(name);

Type guard

function isPDFNameSafe(str) {
  return typeof str === 'string' && !/[^\u0000-\u00ff]/.test(str);
}

Try / catch

try {
  return toPDFName(str);
} catch (e) {
  if (/Invalid PDF Name Object/.test(e.message)) {
    return toPDFName(str.replace(/[^\u0000-\u00ff]/g, ''));
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling toPDFName, or triggering jsPDF internals that call it (font PostScript name embedding in jspdf.js:2014 and font name output in utf8.js), with a string containing multi-byte Unicode (e.g. a font whose name contains CJK characters or an emoji).

Common situations: Registering a custom font whose PostScript/name field contains non-Latin1 characters; a font metadata string with a Unicode trademark or accent beyond Latin-1; passing a user-typed Unicode label into a code path that serializes it as a PDF name.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/1f5ded7be9296c32. Report an issue: GitHub.