parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.__acroform__.arrayToPdfArra

Error message

Invalid argument passed to jsPDF.__acroform__.arrayToPdfArray

What it means

Thrown by arrayToPdfArray when its first argument is not an array. This helper serializes a JavaScript array into a PDF array literal '[...]' for AcroForm field properties (e.g. Opt lists); if the value is not an array it cannot produce valid PDF array syntax and aborts. Exposed at jsPDF.API.__acroform__.arrayToPdfArray.

Source

Thrown at src/modules/acroform.js:775

        case "number":
        case "object":
          content += array[i].toString();
          break;
        case "string":
          if (array[i].substr(0, 1) === "/") {
            content += "/" + pdfEscapeName(array[i].substr(1));
          } else {
            if (typeof objId !== "undefined" && scope)
              encryptor = scope.internal.getEncryptor(objId);
            content += "(" + pdfEscape(encryptor(array[i].toString())) + ")";
          }
          break;
      }
    }
    content += "]";
    return content;
  }
  throw new Error(
    "Invalid argument passed to jsPDF.__acroform__.arrayToPdfArray"
  );
});

function getMatches(string, regex, index) {
  index || (index = 1); // default to the first capturing group
  var matches = [];
  var match;
  while ((match = regex.exec(string))) {
    matches.push(match[index]);
  }
  return matches;
}
var pdfArrayToStringArray = function(array) {
  var result = [];
  if (typeof array === "string") {
    result = getMatches(array, /\((.*?)\)/g);
  }

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Ensure array-typed AcroForm properties receive actual JavaScript arrays.
  2. Wrap scalar values in an array before assignment (e.g. [value]).
  3. Validate with Array.isArray before setting the property.

Example fix

// before
field.Opt = 'Choice A'; // string -> arrayToPdfArray throws on output

// after
field.Opt = ['Choice A', 'Choice B'];
Defensive patterns

Strategy: validation

Validate before calling

function asPdfArray(value) {
  if (!Array.isArray(value)) {
    throw new Error('Expected an array for PDF array serialization, got ' + typeof value);
  }
  return jsPDF.API.__acroform__.arrayToPdfArray(value, objId, scope);
}

Type guard

function isPdfArrayValue(v) {
  return Array.isArray(v);
}

Try / catch

try {
  content = arrayToPdfArray(value, objId, scope);
} catch (e) {
  if (/arrayToPdfArray/.test(e.message)) {
    content = arrayToPdfArray([value], objId, scope); // wrap scalar
  } else throw e;
}

Prevention

When it happens

Trigger: Setting an AcroForm field property that the serializer expects to be an array (e.g. a choice field's Opt) to a non-array value (string, number, object); or calling arrayToPdfArray directly with a scalar.

Common situations: Assigning field.Opt = 'Option1' (a string) instead of field.Opt = ['Option1','Option2']; passing a single value where a list is required; a computed value that returned undefined/null.

Related errors


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