{"record":{"id":"69afeca373dbfa87","repo":"parallax/jsPDF","slug":"atob-error-in-jspdf-convertbase64tobinarystring-e","errorCode":null,"errorMessage":"atob-Error in jsPDF.convertBase64ToBinaryString {e.message}","messagePattern":"atob-Error in jsPDF\\.convertBase64ToBinaryString (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/addimage.js","lineNumber":945,"sourceCode":"    throwError\n  ) {\n    throwError = typeof throwError === \"boolean\" ? throwError : true;\n    var imageData = \"\";\n    var rawData;\n\n    if (typeof stringData === \"string\") {\n      rawData = extractImageFromDataUrl(stringData) ?? stringData;\n\n      try {\n        imageData = atob(rawData);\n      } catch (e) {\n        if (throwError) {\n          if (!validateStringAsBase64(rawData)) {\n            throw new Error(\n              \"Supplied Data is not a valid base64-String jsPDF.convertBase64ToBinaryString \"\n            );\n          } else {\n            throw new Error(\n              \"atob-Error in jsPDF.convertBase64ToBinaryString \" + e.message\n            );\n          }\n        }\n      }\n    }\n    return imageData;\n  });\n\n  /**\n   * @name getImageProperties\n   * @function\n   * @param {Object} imageData\n   * @returns {Object}\n   */\n  jsPDFAPI.getImageProperties = function(imageData) {\n    var image;\n    var tmpImageData = \"\";","sourceCodeStart":927,"sourceCodeEnd":963,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/addimage.js#L927-L963","documentation":"convertBase64ToBinaryString calls atob() on the input. If atob throws but validateStringAsBase64() returns true (the string appears structurally valid base64), this error re-throws the original atob exception with additional context. This indicates the base64 string passes structural validation but atob's native decoder still rejects it — possibly due to browser-specific atob behavior, embedded null bytes, or encoding edge cases.","triggerScenarios":"A base64 string that passes regex/padding checks but contains bytes that the browser's atob rejects. Edge cases in non-standard atob implementations (older Node.js polyfills, specific browser engines). Base64 strings with embedded null characters that survive the regex check. Very long base64 strings that exceed implementation limits.","commonSituations":"Cross-browser differences in atob strictness. Node.js environments using a polyfilled atob that behaves differently from browser atob. Image data with unusual byte patterns from non-standard encoders. WebP or JPEG2000 data with header bytes that confuse atob.","solutions":["Check the e.message in the thrown error for the specific atob failure reason","Try an alternative base64 decoder: Buffer.from(data, 'base64').toString('binary') in Node.js","Re-encode the original binary data to base64 using a reliable encoder to eliminate edge cases","If the data is a data URL, extract only the base64 portion: data.split(',')[1]"],"exampleFix":"// before - atob fails despite valid-looking base64\nvar decoded = doc.convertBase64ToBinaryString(problematicB64);\n\n// after - use Node.js Buffer as alternative decoder\nvar decoded = Buffer.from(problematicB64, 'base64').toString('binary');\n// or re-encode from source\nvar cleanB64 = btoa(originalBinaryString);\nvar decoded = doc.convertBase64ToBinaryString(cleanB64);","handlingStrategy":"try-catch","validationCode":"// Use an alternative decoder when atob may fail\nfunction safeBase64Decode(str) {\n  str = str.replace(/^data:[^;]+;base64,/, '');\n  try {\n    return atob(str);\n  } catch (e) {\n    // Fallback to Buffer (Node.js) or manual decoder\n    if (typeof Buffer !== 'undefined') {\n      return Buffer.from(str, 'base64').toString('binary');\n    }\n    throw e;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  var binary = doc.convertBase64ToBinaryString(data, true);\n} catch (e) {\n  if (e.message.includes('atob-Error')) {\n    // Use alternative base64 decoder\n    var binary = typeof Buffer !== 'undefined'\n      ? Buffer.from(data, 'base64').toString('binary')\n      : atob(data.replace(/[^A-Za-z0-9+/=]/g, ''));\n  } else throw e;\n}","preventionTips":["In Node.js, prefer Buffer.from(data, 'base64') over atob for robustness","Re-encode base64 from the original binary to eliminate edge cases","Check for non-printable characters in base64 strings that confuse atob"],"tags":["addimage","base64","atob","encoding-edge-case"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}