parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.API.__acroform__.setBitFor

Error message

Invalid arguments passed to jsPDF.API.__acroform__.setBitForPdf

What it means

Thrown by setBitForPdf(number, bitPosition) when either argument is NaN. This is the 1-based wrapper over setBit (delegates with bitPosition-1) used by AcroForm boolean setters like showWhenPrinted. Exposed at jsPDF.API.__acroform__.setBitForPdf.

Source

Thrown at src/modules/acroform.js:140

 */
var getBitForPdf = (jsPDFAPI.__acroform__.getBitForPdf = function(
  number,
  bitPosition
) {
  if (isNaN(number) || isNaN(bitPosition)) {
    throw new Error(
      "Invalid arguments passed to jsPDF.API.__acroform__.getBitForPdf"
    );
  }
  return getBit(number, bitPosition - 1);
});

var setBitForPdf = (jsPDFAPI.__acroform__.setBitForPdf = function(
  number,
  bitPosition
) {
  if (isNaN(number) || isNaN(bitPosition)) {
    throw new Error(
      "Invalid arguments passed to jsPDF.API.__acroform__.setBitForPdf"
    );
  }
  return setBit(number, bitPosition - 1);
});

var clearBitForPdf = (jsPDFAPI.__acroform__.clearBitForPdf = function(
  number,
  bitPosition
) {
  if (isNaN(number) || isNaN(bitPosition)) {
    throw new Error(
      "Invalid arguments passed to jsPDF.API.__acroform__.clearBitForPdf"
    );
  }
  return clearBit(number, bitPosition - 1);
});

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Ensure number (the flag accumulator) and bitPosition are finite numbers, bitPosition >= 1.
  2. Never set F/Ff to non-numeric values before toggling boolean properties.
  3. Use the boolean property setters, which supply correct positions, rather than calling setBitForPdf with a computed index.

Example fix

// before
field.showWhenPrinted = true; // _F is NaN -> setBitForPdf throws

// after
if (typeof field.F === 'number' && !isNaN(field.F)) {
  field.showWhenPrinted = true;
}
Defensive patterns

Strategy: type-guard

Validate before calling

function safeSetBitForPdf(number, bitPosition) {
  number = Number(number);
  var p = Number(bitPosition);
  if (!isFinite(number) || !isFinite(p) || p < 1) {
    throw new Error('setBitForPdf needs finite number and bitPosition>=1');
  }
  return jsPDF.API.__acroform__.setBitForPdf(number, p);
}

Type guard

function areFiniteNumbers(a, b) {
  return typeof a === 'number' && isFinite(a) &&
         typeof b === 'number' && isFinite(b) && b >= 1;
}

Try / catch

try {
  field.F = jsPDF.API.__acroform__.setBitForPdf(field.F, pos);
} catch (e) {
  if (/__acroform__.setBitForPdf/.test(e.message)) {
    if (typeof field.F !== 'number' || isNaN(field.F)) field.F = 4;
    field.F = jsPDF.API.__acroform__.setBitForPdf(field.F, pos);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling setBitForPdf with NaN, or toggling an AcroForm boolean property whose underlying _F flag is NaN (so the 'number' argument is NaN) or whose PDF bit position is missing.

Common situations: Setting field.showWhenPrinted or similar after a bad F assignment; programmatic flag building with unvalidated positions.

Related errors


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