Fill PDF Forms Programmatically Using JavaScript

PSPDFKit for Web offers multiple options for filling form fields programmatically, each with a different set of tradeoffs.

PSPDFKit Server

With the Server-backed deployment option, any time form fields are filled using the UI or the API, the changes are automatically sent and stored by PSPDFKit Server. When a document is opened from Server, the latest form field values are immediately loaded. Moreover, if you use Instant, any form field changes made by any of the connected users will be immediately synchronized to all other users.

In addition, PSPDFKit Server exposes an HTTP-based API that you can use to fill the form fields in any document, and those changes will immediately be available to all your users.

Read more about this form filling method in the Import from PSPDFKit Server guide.

Instant JSON

Instant JSON is a data format that encodes annotations, bookmarks, and form field values in a compact JSON format. With PSPDFKit for Web Standalone, you can prefill the form field values in a document by providing the Instant JSON object when the document is opened.

Using Instant JSON to fill in the form fields is an efficient way of restoring any form field value changes that a user made when working on the document the last time. Instead of saving and loading the whole document with changes, which might take a lot of space, you can export the Instant JSON file when the user finishes working on the document and import it when they open the document again.

Read more about this form filling method in the Import from Instant JSON guide.

XFDF

XFDF is a data format that encodes annotations and form field values as an XML document. With PSPDFKit for Web Standalone, you can prefill the form field values in a document by providing the XFDF file when the document is opened.

XFDF enables interoperability with other PDF software, since it’s often used as an annotation and form field value export format by these tools. You could export form field values as XFDF from a PDF viewer application and then import them into PSPDFKit for Web.

The downside of using XFDF compared to Instant JSON is that it encodes a snapshot of all annotations and form field values in the document, whereas Instant JSON only represents a difference between the data in the file and the changes the user made. In the case of files with a lot of annotations and form fields, an Instant JSON payload can be significantly smaller than its XFDF equivalent.

Read more about form filling with XFDF in the Import from XFDF guide.

Database

You can easily fill in the form fields in a document with the data stored in your database or a backend application. Since Instant JSON is a documented data format, you can pull the form field values from your database, assemble an Instant JSON object, and pass it to PSPDFKit for Web when opening a document. This is a great approach when you want to integrate PSPDFKit for Web in an application where you already have existing data. Read our guide on importing from a database to learn more.

Note that if you use the Server-backed deployment option, you can fill the form fields using PSPDFKit Server’s forms API. This will allow you to fill the form fields only once, as any changes you make via the Server API will be immediately available to all users opening documents from PSPDFKit Server.