{"record":{"id":"813e769de34c8d91","repo":"parallax/jsPDF","slug":"invalid-argument-passed-to-jspdf-addfield","errorCode":null,"errorMessage":"Invalid argument passed to jsPDF.addField.","messagePattern":"Invalid argument passed to jsPDF\\.addField\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/acroform.js","lineNumber":3146,"sourceCode":"\n// Public:\n\n/**\n * Add an AcroForm-Field to the jsPDF-instance\n *\n * @name addField\n * @function\n * @instance\n * @param {Object} fieldObject\n * @returns {jsPDF}\n */\nvar addField = (jsPDFAPI.addField = function(fieldObject) {\n  initializeAcroForm(this, fieldObject);\n\n  if (fieldObject instanceof AcroFormField) {\n    putForm(fieldObject);\n  } else {\n    throw new Error(\"Invalid argument passed to jsPDF.addField.\");\n  }\n  fieldObject.page = fieldObject.scope.internal.getCurrentPageInfo().pageNumber;\n  return this;\n});\n\njsPDFAPI.AcroFormChoiceField = AcroFormChoiceField;\njsPDFAPI.AcroFormListBox = AcroFormListBox;\njsPDFAPI.AcroFormComboBox = AcroFormComboBox;\njsPDFAPI.AcroFormEditBox = AcroFormEditBox;\njsPDFAPI.AcroFormButton = AcroFormButton;\njsPDFAPI.AcroFormPushButton = AcroFormPushButton;\njsPDFAPI.AcroFormRadioButton = AcroFormRadioButton;\njsPDFAPI.AcroFormCheckBox = AcroFormCheckBox;\njsPDFAPI.AcroFormTextField = AcroFormTextField;\njsPDFAPI.AcroFormPasswordField = AcroFormPasswordField;\njsPDFAPI.AcroFormAppearance = AcroFormAppearance;\n\njsPDFAPI.AcroForm = {","sourceCodeStart":3128,"sourceCodeEnd":3164,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/acroform.js#L3128-L3164","documentation":"jsPDF.addField() registers an AcroForm field into the PDF document. It uses instanceof AcroFormField to verify the argument is a proper form field object created through the AcroForm API (e.g., new AcroFormTextField(), new AcroFormCheckBox()). Plain objects or field-like literals will fail this check because they lack the internal prototype chain and initialization that AcroFormField provides.","triggerScenarios":"Calling doc.addField({}) or doc.addField(plainObject). Calling doc.addField(new SomeCustomClass()) that is not a subclass of AcroFormField. Creating fields with Object.create() without proper prototype linkage. Importing field objects from JSON (deserialization loses the prototype chain).","commonSituations":"Developers trying to construct field objects manually instead of using the factory constructors. Serializing fields to JSON and deserializing them, which strips the instanceof relationship. Using a different/older version of jsPDF where the AcroFormField class reference differs.","solutions":["Create fields using the provided constructors: var field = new doc.AcroFormTextField() or new AcroFormCheckBox()","Ensure you import AcroFormField subclasses from the same jsPDF instance that addField is called on","Do not serialize/deserialize field objects; reconstruct them from data","If using multiple jsPDF bundles, ensure field objects and addField come from the same bundle instance"],"exampleFix":"// before\ndoc.addField({ fieldName: 'myField', FT: '/Tx' }); // throws\n\n// after\nvar textField = new doc.AcroFormTextField();\ntextField.fieldName = 'myField';\ndoc.addField(textField);","handlingStrategy":"type-guard","validationCode":"// Verify field is an AcroFormField before calling addField\nfunction isAcroFormField(obj, jsPDFInstance) {\n  // Check for the constructor name or key AcroFormField properties\n  return obj instanceof jsPDFInstance.AcroFormFieldConstructor ||\n    (obj && typeof obj === 'object' && 'FT' in obj && 'fieldName' in obj);\n}\n\n// Safer: always create via constructors\nvar field = new doc.AcroFormTextField();\ndoc.addField(field);","typeGuard":"/**\n * Check if object is likely an AcroFormField instance\n * @param {*} obj\n * @returns {boolean}\n */\nfunction looksLikeAcroFormField(obj) {\n  return obj != null &&\n    typeof obj === 'object' &&\n    typeof obj.hasOwnProperty === 'function' &&\n    ('FT' in obj || 'fieldName' in obj || 'partialFieldNames' in obj);\n}","tryCatchPattern":"try {\n  doc.addField(fieldObject);\n} catch (e) {\n  console.error('addField failed - object is not an AcroFormField:', e.message);\n  // Reconstruct the field properly\n  var newField = new doc.AcroFormTextField();\n  Object.assign(newField, fieldObject);\n  doc.addField(newField);\n}","preventionTips":["Always create fields via constructors: new doc.AcroFormTextField(), new doc.AcroFormCheckBox(), etc.","Never serialize field objects to JSON and back — the prototype chain is lost","Ensure field objects and addField come from the same jsPDF instance/bundle"],"tags":["acroform","instanceof","field-registration","type-check"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}