{"record":{"id":"efaf69bc46309209","repo":"parallax/jsPDF","slug":"supplied-data-is-not-a-valid-base64-string-jspdf-c","errorCode":null,"errorMessage":"Supplied Data is not a valid base64-String jsPDF.convertBase64ToBinaryString ","messagePattern":"Supplied Data is not a valid base64-String jsPDF\\.convertBase64ToBinaryString ","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/addimage.js","lineNumber":941,"sourceCode":"   * @returns {string} binary string\n   */\n  var convertBase64ToBinaryString = (jsPDFAPI.__addimage__.convertBase64ToBinaryString = function(\n    stringData,\n    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}","sourceCodeStart":923,"sourceCodeEnd":959,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/addimage.js#L923-L959","documentation":"convertBase64ToBinaryString attempts to decode the input with atob(). If atob throws, the function runs validateStringAsBase64() to determine the cause. If validation fails (length not multiple of 4, invalid characters, bad padding), this error is thrown indicating the input is not valid base64. The function also strips data-URL prefixes via extractImageFromDataUrl before validation.","triggerScenarios":"Passing a string with non-base64 characters (spaces, special chars, HTML entities). Passing a string whose length is not a multiple of 4. Passing a data URL where the base64 portion is malformed. Passing a raw file path or URL string that is not base64 at all. Passing a string with incorrect padding (= or ==).","commonSituations":"Copying base64 strings from web pages where whitespace or line breaks were inserted. Concatenating base64 chunks incorrectly. Data URLs with missing 'base64,' prefix. Encoding issues where UTF-8 characters leak into the base64 string. Server-side responses with Content-Transfer-Encoding issues.","solutions":["Strip whitespace and line breaks before passing: data.replace(/\\s/g, '')","If passing a data URL, ensure it has the format: data:image/png;base64,<base64data>","Validate before calling: use jsPDF.API.__addimage__.validateStringAsBase64(data) to check","Pass throwError=false to get an empty string instead of an error, then handle the empty result"],"exampleFix":"// before\nvar dirty = 'data:image/png;base64, iVBORw0KGgo...\\n';\ndoc.convertBase64ToBinaryString(dirty); // throws - has spaces/newlines\n\n// after\nvar clean = dirty.replace(/\\s/g, '');\ndoc.convertBase64ToBinaryString(clean);\n// or let addImage handle it: doc.addImage(dirty.trim(), 'PNG', 10, 10);","handlingStrategy":"validation","validationCode":"// Validate base64 before calling convertBase64ToBinaryString\nfunction isValidBase64(str) {\n  if (typeof str !== 'string') return false;\n  str = str.replace(/^data:[^;]+;base64,/, '').trim();\n  str = str.replace(/\\s/g, '');\n  return str.length > 0 &&\n    str.length % 4 === 0 &&\n    /^[A-Za-z0-9+/]+={0,2}$/.test(str);\n}\n\nvar cleanData = rawData.replace(/\\s/g, '');\nif (isValidBase64(cleanData)) {\n  doc.convertBase64ToBinaryString(cleanData);\n}","typeGuard":"/**\n * @param {*} str\n * @returns {boolean}\n */\nfunction isBase64String(str) {\n  return typeof str === 'string' &&\n    str.length % 4 === 0 &&\n    /^[A-Za-z0-9+/]+={0,2}$/.test(str.trim().replace(/\\s/g, ''));\n}","tryCatchPattern":"try {\n  var binary = doc.convertBase64ToBinaryString(data, true);\n} catch (e) {\n  // Try with throwError=false to get empty string, then use alternative\n  var binary = doc.convertBase64ToBinaryString(data, false);\n  if (!binary) {\n    binary = Buffer.from(data, 'base64').toString('binary'); // Node.js fallback\n  }\n}","preventionTips":["Strip whitespace and newlines from base64 strings before processing","Use jsPDF.API.__addimage__.validateStringAsBase64(data) for pre-validation","Pass throwError=false to convertBase64ToBinaryString for non-throwing behavior"],"tags":["addimage","base64","encoding","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}