X Enterprises
fastify-xpdf

listFormFields

Enumerate all form fields in a PDF — name, type, and current value.

listFormFields

List all interactive form fields in a PDF using pdf-lib. Returns the field name, type, and current value for each field. Useful for inspecting a template before calling fillForm.

Signature

fastify.xPDF.listFormFields(pdfBuffer: Buffer): Promise<FormField[]>

interface FormField {
  name: string
  type: "text" | "checkbox" | "radio" | "dropdown" | "option" | "button" | "signature" | "unknown"
  value: string | boolean | null
}

Params

NameTypeRequiredDescription
pdfBufferBufferYesPDF buffer to inspect

Returns

FormField[] — an array of all fields. Empty array if the PDF has no form fields.

Throws

ErrorWhen
[xPDF] Invalid PDF bufferpdfBuffer doesn't start with %PDF

Examples

Inspect a template before filling

fastify.get("/templates/:name/fields", async (request, reply) => {
  const template = await fetchTemplateBuffer(`${request.params.name}.pdf`);
  const fields = await fastify.xPDF.listFormFields(template);

  return {
    fieldCount: fields.length,
    fields: fields.map(({ name, type }) => ({ name, type })),
  };
});

// Response:
// {
//   "fieldCount": 5,
//   "fields": [
//     { "name": "firstName", "type": "text" },
//     { "name": "lastName", "type": "text" },
//     { "name": "agreeToTerms", "type": "checkbox" },
//     { "name": "country", "type": "dropdown" },
//     { "name": "signature", "type": "signature" }
//   ]
// }

Validate that all required fields are present before filling

async function fillChecked(templateBuffer, values) {
  const fields = await fastify.xPDF.listFormFields(templateBuffer);
  const fieldNames = new Set(fields.map((f) => f.name));

  const missing = Object.keys(values).filter((k) => !fieldNames.has(k));
  if (missing.length > 0) {
    throw new Error(`Unknown field names: ${missing.join(", ")}`);
  }

  return fastify.xPDF.fillForm(templateBuffer, values);
}

See Also

  • fillForm — Fill field values returned by this method

AI Context

package: "@xenterprises/fastify-xpdf"
method: fastify.xPDF.listFormFields(pdfBuffer)
use-when: Enumerate all form fields in a PDF template with their names, types, and current values
params: pdfBuffer (Buffer)
returns: Array<{ name, type, value, required }>
Copyright © 2026