{"record":{"id":"1f5ded7be9296c32","repo":"parallax/jsPDF","slug":"invalid-pdf-name-object-str-only-accept","errorCode":null,"errorMessage":"Invalid PDF Name Object: \" + str + \", Only accept ASCII characters.","messagePattern":"Invalid PDF Name Object: \" \\+ str \\+ \", Only accept ASCII characters\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/libs/pdfname.js","lineNumber":10,"sourceCode":"/**\n * Convert string to `PDF Name Object`.\n * Detail: PDF Reference 1.3 - Chapter 3.2.4 Name Object\n * @param str\n */\nfunction toPDFName(str) {\n  // eslint-disable-next-line no-control-regex\n  if (/[^\\u0000-\\u00ff]/.test(str)) {\n    // non ascii string\n    throw new Error(\n      \"Invalid PDF Name Object: \" + str + \", Only accept ASCII characters.\"\n    );\n  }\n  var result = \"\",\n    strLength = str.length;\n  for (var i = 0; i < strLength; i++) {\n    var charCode = str.charCodeAt(i);\n    if (\n      charCode < 0x21 ||\n      charCode === 0x23 /* # */ ||\n      charCode === 0x25 /* % */ ||\n      charCode === 0x28 /* ( */ ||\n      charCode === 0x29 /* ) */ ||\n      charCode === 0x2f /* / */ ||\n      charCode === 0x3c /* < */ ||\n      charCode === 0x3e /* > */ ||\n      charCode === 0x5b /* [ */ ||\n      charCode === 0x5d /* ] */ ||","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/libs/pdfname.js#L1-L28","documentation":"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 /[^\u0000-ÿ]/ actually permits the full Latin-1 range and only blocks code points > 0xFF such as CJK, Cyrillic, or emoji.)","triggerScenarios":"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).","commonSituations":"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.","solutions":["Strip or transliterate characters above U+00FF from the font/name string before it reaches jsPDF.","Keep font PostScript names ASCII-only when generating/converting the font (recommended PDF practice anyway).","If you must keep the characters, encode the name yourself as #-escaped Latin-1 bytes instead of relying on toPDFName for the raw string."],"exampleFix":"// before\nout(\"/BaseFont /\" + toPDFName(font.postScriptName)); // postScriptName has CJK\n\n// after\nvar safeName = font.postScriptName.replace(/[^\\u0000-\\u00ff]/g, '');\nout(\"/BaseFont /\" + toPDFName(safeName));","handlingStrategy":"validation","validationCode":"function isLatin1(str) {\n  return typeof str === 'string' && !/[^\\u0000-\\u00ff]/.test(str);\n}\nif (!isLatin1(name)) {\n  name = name.replace(/[^\\u0000-\\u00ff]/g, ''); // or transliterate\n}\ntoPDFName(name);","typeGuard":"function isPDFNameSafe(str) {\n  return typeof str === 'string' && !/[^\\u0000-\\u00ff]/.test(str);\n}","tryCatchPattern":"try {\n  return toPDFName(str);\n} catch (e) {\n  if (/Invalid PDF Name Object/.test(e.message)) {\n    return toPDFName(str.replace(/[^\\u0000-\\u00ff]/g, ''));\n  }\n  throw e;\n}","preventionTips":["Keep font PostScript/name fields ASCII-only.","Strip or transliterate code points above U+00FF before they reach jsPDF internals.","Validate user-supplied names with a Latin-1 check before embedding."],"tags":["pdf","unicode","validation","font","pdfname"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}