Instant JSON

Instant JSON is our approach to bringing annotations into a modern format while keeping all important properties to make the Instant JSON spec work with PDF. It is fully documented and supports long-term storage.

Instant JSON stores PDF changes like annotations in a separate JSON file. This means that a PDF document will only need to be transferred once and all changes will be added as an overlay to the existing PDF. This approach significantly reduces the bandwidth since you only need to transfer this JSON instead of the complete PDF.

Conceptually, Instant JSON defines a list of skippedPdfObjectIds. These point to the PDF’s internal object IDs for annotations. Whenever an object ID is marked as skipped, it will no longer be loaded from the original PDF. Instead, it could be defined inside the annotations array with the same pdfObjectId. If this is the case, the PDF viewer will display the new annotation, which signals an update to the original one. If an object ID is marked as skipped but the annotations array does not contain an annotation with the same pdfObjectId, it will be interpreted as a deleted annotation. An annotation that is inside the annotations array without the pdfObjectId property is interpreted as a newly created annotation.

All annotations in the annotations array have a unique id field. For updated annotations that were already in the original PDF, this field will be the stringified pdfObjectId. Newly created annotations will get a newly generated ULID.

An “empty” Instant JSON contains neither skippedPdfObjectIds nor annotations, which means the original PDF is untouched. All annotations in the initial PDF are still shown.

The Format

We use Flow type declarations to specify the format of Instant JSON:

Copy
1
2
3
4
5
6
7
8
9
10
declare type InstantJSON = {
  format: "https://pspdfkit.com/instant-json/v1",
  pdfId?: {
    permanent: string,
    changing: string
  },
  skippedPdfObjectIds?: number[],
  annotations?: Object[],
  formFieldValues?: Object[]
};

format

This is a literal string that includes the version information.

pdfId

This optional key contains an object of a permanent PDF ID and a changing PDF ID. According to the PDF spec, a PDF document must contain these IDs. We use the permanent ID to verify that the PDF you’ve opened together with this Instant JSON is indeed the correct one. The changing PDF ID will be updated whenever the PDF file is updated (for example, when saved with different annotations). Since Instant JSON only works with an immutable PDF, the state will be invalid when used with a changed PDF.

Not every PDF will have a valid permanent or changing ID. As such, this field might not be set. We recommend you take care to always use the same PDF.

skippedPdfObjectIds

This is an array of PDF object IDs that will be ignored when importing annotations from the original PDF document. If this array is empty, the key should not be set.

annotations

This is a list of new or updated annotations. Annotations follow the format for Instant JSON for annotations. When an annotation contains a pdfObjectId, it is considered to be an update to one of the annotations of the original PDF. For newly created annotations, this key will not be set.

formFieldValues

This is a list of modified form field values. Objects follow the Instant form field value JSON format. This list will not be preset when no form field values have been modified.

Instant Document JSON API

Exporting Instant JSON

You can export the current annotations of a document as an Instant JSON file via a GET request to /api/documents/:document_id/document.json. To get the current annotation of a layer, send a GET request to /api/documents/:document_id/layers/:layer_name/document.json:

Request

1
2
GET /api/documents/:document_id/document.json
Authorization: Token token="<secret token>"
1
2
$ curl "http://localhost:5000/api/documents/abc/document.json" \
   -H "Authorization: Token token=<secret token>"

Response

1
2
3
4
HTTP/1.1 200 OK
Content-Type: application/json

<JSON data>

Importing Instant JSON

PSPDFKit Server supports importing annotations from an Instant JSON file when uploading a document via the /api/documents endpoint. Send a multipart/form-data POST request, including the PDF and the Instant JSON file, to import the annotations in the given PDF file. This will replace all existing annotations in the uploaded PDF with the annotations from the uploaded Instant JSON file. If you want to add annotations to already existing ones instead of replacing them, you can set keep_current_annotations to true:

Request

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
POST /api/documents
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token="<secret token>"

--customboundary
Content-Disposition: form-data; name="file"; filename="Example Document.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="attachment"; filename="attachment.json"
Content-Type: application/json

<annotations data>
--customboundary

Importing Instant JSON on Existing Documents

PSPDFKit Server supports importing annotations from an Instant JSON file on an existing document via two different endpoints:

  • POST /pdf_with_instant_json receives an instant.json file and responds with the resulting PDF of the document/layer and the imported instant.json.
  • POST /copy_with_instant_json can optionally receive an instant.json file and will persist the resulting PDF of the document/layer and the imported instant.json file as a new document. The JSON response will include the document_id of the document.

Here’s an Instant JSON sample payload for a document with an ink annotation:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
  "annotations": [
    {
      "v": 1,
      "type": "pspdfkit/ink",
      "id": "01CHG7QMTDT1JFYQ8BSKGT6F3P",
      "bbox": [97.5, 97.5, 155, 205],
      "blendMode": "normal",
      "createdAt": "2018-07-03T14:11:21Z",
      "creatorName": "John Appleseed",
      "isDrawnNaturally": false,
      "lineWidth": 5,
      "lines": {
        "intensities": [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]],
        "points": [
          [[100, 100], [100, 200], [150, 300]],
          [[200, 100], [200, 200], [250, 300]]
        ]
      },
      "name": "A167811E-6D10-4546-A147-B7AD775FE8AC",
      "note": "",
      "opacity": 1,
      "pageIndex": 0,
      "strokeColor": "#AA47BE",
      "updatedAt": "2018-07-03T14:11:21Z"
    }
  ],
  "format": "https://pspdfkit.com/instant-json/v1",
  "pdfId": {
    "changing": "wljL9fB/TPOAuGjHAsAsHg==",
    "permanent": "qTwUmg5VSm6ysfzcPlFvnQ=="
  },
  "skippedPdfObjectIds": [557]
}