parallax/jsPDF · error · Error

Supplied Data is not a valid base64-String jsPDF.convertBase

Error message

Supplied Data is not a valid base64-String jsPDF.convertBase64ToBinaryString 

What it means

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.

Source

Thrown at src/modules/addimage.js:941

   * @returns {string} binary string
   */
  var convertBase64ToBinaryString = (jsPDFAPI.__addimage__.convertBase64ToBinaryString = function(
    stringData,
    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}

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Strip whitespace and line breaks before passing: data.replace(/\s/g, '')
  2. If passing a data URL, ensure it has the format: data:image/png;base64,<base64data>
  3. Validate before calling: use jsPDF.API.__addimage__.validateStringAsBase64(data) to check
  4. Pass throwError=false to get an empty string instead of an error, then handle the empty result

Example fix

// before
var dirty = 'data:image/png;base64, iVBORw0KGgo...\n';
doc.convertBase64ToBinaryString(dirty); // throws - has spaces/newlines

// after
var clean = dirty.replace(/\s/g, '');
doc.convertBase64ToBinaryString(clean);
// or let addImage handle it: doc.addImage(dirty.trim(), 'PNG', 10, 10);
Defensive patterns

Strategy: validation

Validate before calling

// Validate base64 before calling convertBase64ToBinaryString
function isValidBase64(str) {
  if (typeof str !== 'string') return false;
  str = str.replace(/^data:[^;]+;base64,/, '').trim();
  str = str.replace(/\s/g, '');
  return str.length > 0 &&
    str.length % 4 === 0 &&
    /^[A-Za-z0-9+/]+={0,2}$/.test(str);
}

var cleanData = rawData.replace(/\s/g, '');
if (isValidBase64(cleanData)) {
  doc.convertBase64ToBinaryString(cleanData);
}

Type guard

/**
 * @param {*} str
 * @returns {boolean}
 */
function isBase64String(str) {
  return typeof str === 'string' &&
    str.length % 4 === 0 &&
    /^[A-Za-z0-9+/]+={0,2}$/.test(str.trim().replace(/\s/g, ''));
}

Try / catch

try {
  var binary = doc.convertBase64ToBinaryString(data, true);
} catch (e) {
  // Try with throwError=false to get empty string, then use alternative
  var binary = doc.convertBase64ToBinaryString(data, false);
  if (!binary) {
    binary = Buffer.from(data, 'base64').toString('binary'); // Node.js fallback
  }
}

Prevention

When it happens

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

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

Related errors


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