Fill In PDF Form Fields Using pdf-lib

With SurveyJS PDF Generator, you can fill interactive fields in existing PDF forms—such as job applications, registration forms, or any standardized forms—with survey data. This helps automate data entry and streamline document workflows. To enable this functionality, you'll need a PDF document with empty fields, a data object produced by a SurveyJS survey, and an object that maps survey fields to PDF form fields. You'll also need a third-party library, such as pdf-lib.

Add the pdf-lib Library

Depending on whether you have a modular or classic script application, add the pdf-lib library to it as follows:

  • Option 1: Reference the pdf-lib script on your HTML page.

    <head>
      <!-- ... -->
      <script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js"></script>
      <!-- ... -->
    </head>
    
  • Option 2: Install the pdf-lib npm package and import the entire pdf-lib module.

    npm install pdf-lib --save
    
    import * as PDFLib from "pdf-lib";
    

Configure the PDFFormFiller Plugin

SurveyJS PDF Generator integrates with a third-party library using the PDFFormFiller plugin. To add it to your application, use the same options as with pdf-lib:

  • Option 1: Reference the pdf-form-filler script on your HTML page.

    <head>
      <!-- ... -->
      <script src="https://unpkg.com/survey-pdf/pdf-form-filler.min.js"></script>
      <!-- ... -->
    </head>
    
  • Option 2: Install the survey-pdf npm package and import PDFFormFiller from the survey-pdf/pdf-form-filler module.

    npm install survey-pdf --save
    
    import { PDFFormFiller } from "survey-pdf/pdf-form-filler";
    

To configure the PDFFormFiller plugin, pass a configuration object with the following properties to its constructor:

  • pdfLibraryAdapter
    An adapter serves as a bridge between the plugin and a specific third-party library. For pdf-lib, you need to use the PDFLibAdapter, which is part of the pdf-form-filler script/module. Instantiate the PDFLibAdapter by passing the PDFLib object to its constructor and assign the instance to the pdfLibraryAdapter property.

  • pdfTemplate
    A PDF document with interactive fields that you want to fill. You can load it from a server or encode the document to a Base64 data URL and embed it in your code.

  • data
    An object with data used to populate the PDF document. Use the SurveyModel's data property to access this data object.

  • fieldMap
    An object that maps survey fields to PDF form fields. Object keys are survey field names and object values are PDF form field IDs. The easiest way to build a field map is to access the data object with respondent answers using the SurveyModel's data property and replace the values with the PDF form field IDs. To find the IDs, open your PDF document in any editor that allows viewing them. Note that certain field types, such as Checkboxes, Dynamic Matrix, and Dynamic Panel require a different configuration. Refer to the fieldMap object in code for an example.

The following code shows a simple example of PDFFormFiller configuration:

// ...
const pdfTemplate = "data:application/pdf;base64,...";
const data = {
  "employer": "ABC Technologies",
  "position": "Software Developer",
  "name": "Doe, Jane Marie",
  // ...
}
const fieldMap = {
  "employer": "Employer",
  "position": "Position",
  "name": "Candidate Name",
  // ...
}
const form = new PDFFormFiller({
  pdfLibraryAdapter: new PDFLibAdapter(PDFLib),
  pdfTemplate: pdfTemplate,
  data: data,
  fieldMap: fieldMap
});

Save the Filled In PDF Form

To save the PDF document with populated interactive fields on a user's storage, call the PDFFormFiller's save(name) method:

form.save("FilledForm.pdf");

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.