parallax/jsPDF · error · Error

atob-Error in jsPDF.convertBase64ToBinaryString

Error message

atob-Error in jsPDF.convertBase64ToBinaryString {e.message}

What it means

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.

Solutions

  1. Check the e.message in the thrown error for the specific atob failure reason
  2. Try an alternative base64 decoder: Buffer.from(data, 'base64').toString('binary') in Node.js
  3. Re-encode the original binary data to base64 using a reliable encoder to eliminate edge cases
  4. If the data is a data URL, extract only the base64 portion: data.split(',')[1]

Example fix

// before - atob fails despite valid-looking base64
var decoded = doc.convertBase64ToBinaryString(problematicB64);

// after - use Node.js Buffer as alternative decoder
var decoded = Buffer.from(problematicB64, 'base64').toString('binary');
// or re-encode from source
var cleanB64 = btoa(originalBinaryString);
var decoded = doc.convertBase64ToBinaryString(cleanB64);
Defensive patterns

Strategy: try-catch

Validate before calling

// Use an alternative decoder when atob may fail
function safeBase64Decode(str) {
  str = str.replace(/^data:[^;]+;base64,/, '');
  try {
    return atob(str);
  } catch (e) {
    // Fallback to Buffer (Node.js) or manual decoder
    if (typeof Buffer !== 'undefined') {
      return Buffer.from(str, 'base64').toString('binary');
    }
    throw e;
  }
}

Try / catch

try {
  var binary = doc.convertBase64ToBinaryString(data, true);
} catch (e) {
  if (e.message.includes('atob-Error')) {
    // Use alternative base64 decoder
    var binary = typeof Buffer !== 'undefined'
      ? Buffer.from(data, 'base64').toString('binary')
      : atob(data.replace(/[^A-Za-z0-9+/=]/g, ''));
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/modules/addimage.js:945

    throwError
  ) {
    throwError = typeof throwError === "boolean" ? throwError : true;
    var imageData = "";
    var rawData;

    if (typeof stringData === "string") {
      rawData = extractImageFromDataUrl(stringData) ?? stringData;

      try {
        imageData = atob(rawData);
      } catch (e) {
        if (throwError) {
          if (!validateStringAsBase64(rawData)) {
            throw new Error(
              "Supplied Data is not a valid base64-String jsPDF.convertBase64ToBinaryString "
            );
          } else {
            throw new Error(
              "atob-Error in jsPDF.convertBase64ToBinaryString " + e.message
            );
          }
        }
      }
    }
    return imageData;
  });

  /**
   * @name getImageProperties
   * @function
   * @param {Object} imageData
   * @returns {Object}
   */
  jsPDFAPI.getImageProperties = function(imageData) {
    var image;
    var tmpImageData = "";

View on GitHub (pinned to a3930ce03a)