First-class JSON support with DocJSON
DocJSON is the internal format used by Document Authoring, and it’s designed for high portability and custom integrations. This document representation retains the good parts of existing formats, but it fits better with modern tooling and APIs that often exchange data in JSON. An intermediary format was important for us to maximize the versatility of the library, to integrate better with web technologies, and to provide the best possible developer experience.
DocJSON is used when loading and saving from the editor. The format is easy to understand, and it can be modified outside the editor to fit your custom workflows, including templating and document generation. If you can modify JSON on the client or server, you can generate documents that the Document Authoring library can open and export (in multiple formats).
It’s our intention to fully document the format and push document handling toward more modern alternatives, with better interoperability. You can see a simple example of the format at the end of this guide.
Working with DocJSON in the editor
Before you start, ensure you have the Document Authoring library installed and running. Check out the getting started guides for more information.
The example code omits error handling. Add it before using this code in production.
Loading DocJSON
Use the DocAuthSystem.loadDocument method:
// Assuming the `docAuthSystem` instance exists.
const docAuthDocument = await docAuthSystem.loadDocument( await fetch('./document.json').then((response) => response.json()),);
const editor = await docAuthSystem.createEditor( document.getElementById('editor'), { document: docAuthDocument },);Saving DocJSON
Use the DocAuthDocument.saveDocument or DocAuthDocument.saveDocumentJSONString method:
// Assuming the `editor` instance exists.
const currentDoc = editor.currentDocument();
const docObj = await currentDoc.saveDocument();console.log('JS object', docObj);
const docJSON = await currentDoc.saveDocumentJSONString();console.log('JSON string', docJSON);Example
The example below demonstrates a minimal valid DocJSON document for the current format version. It contains a single paragraph with one text run of Hello world!, plus the trailing section properties required by the current format.
{ "type": "https://pspdfkit.com/document-authoring/persistence/container", "version": 14, "container": { "document": { "body": { "elements": [ { "type": "p", "elements": [ { "type": "r", "text": "Hello world!" } ] } ], "finalSectionPr": { "pageSetup": { "margins": { "l": 72, "r": 72, "t": 72, "b": 72 }, "size": { "x": 595, "y": 842 } }, "footnotePr": { "separator": [], "numberFormat": "decimal", "restartRule": "none", "startAt": 1 }, "endnotePr": { "separator": [], "numberFormat": "upperRoman", "restartRule": "none", "startAt": 1 } } } } }}Exploring more
Older DocJSON documents that use body.sections are upgraded automatically when loaded. If your integration constructs or reads raw DocJSON directly, use the flat body.elements shape for new documents.
If you’re interested in diving deeper into the DocJSON format, reach out to us.