Transferring File Edits to a Server

Document JSON is a serializable representation of the current changes to a document, i.e. a diff between the Document’s saved and unsaved changes. This can be used to transfer a set of changes across devices without having to send the entire PDF, which could potentially be very large. PSPDFKit for Web uses this in standalone deployment to reduce bandwidth use. Currently, the generated JSON only contains changes to annotations.

To generate Instant JSON for a document, call Document.generateInstantJSON(from:) on the document from which you wish to retrieve currently unsaved changes in JSON form. Note that this method will return nil if there are no unsaved changes in the document:

let data = try? document.generateInstantJSON(from: document.documentProviders.first)

This generated data can be sent using an HTTP request to your server. Similarly, you can fetch the JSON data from your server, and it can then be applied to a document using Document.applyInstantJSON(fromDataProvider:to:lenient:). If you have an NSData object containing the Document JSON data, create a DataContainerProvider and pass that as the dataProvider argument to the method, like this:

let document = ...
let jsonContainer = DataContainerProvider(data:data!)
do {
    try document.applyInstantJSON(fromDataProvider: jsonContainer, to: document.documentProviders.first, lenient: false)
} catch {
    print("Error: \(error).")

For more details, see the Instant JSON — Document example from InstantJSONExamples.swift in PSPDFKit Catalog.