Accusoft PDF Viewer Documentation

v3.11.158639

How To View a Viewing Package

This guide explains how to view a document as a Viewing Package using PrizmDoc Viewer (requires PrizmDoc Viewer v13.20 or later).

Prerequisites

Before using PDF Viewer to view a Viewing Package, a reverse proxy must be established to route image handling requests. Afterwards, document Viewing Packages may be viewed with PDF Viewer. If you have not yet configured a reverse proxy for PrizmDoc Server, see the section How To Configure a Reverse Proxy for PrizmDoc Server before attempting to view a Viewing Package with PDF Viewer.

Directions

PrizmDoc Viewer (v13.20+) can create Viewing Packages for many different document types, and these Viewing Packages can be viewed by PDF Viewer. The range of viewable document types can be broadly expanded for PDF Viewer through the use of Viewing Packages.

The general process is as follows:

  • First, upload a document to a PrizmDoc Server, then create a Viewing Package for the document.
  • Once the Viewing Package exists, a Viewing Session can be created for the Viewing Package.
  • Finally, the PDF Viewer can request it from the PrizmDoc Server for viewing.

Viewing Packages can be used indefinitely for many viewing sessions, so the creation process is only required once per document.

NOTE: It is important that all of the work up to providing PDF Viewer with a Viewing Session should be done on server side and not within the browser. This is done primarily to control resource creation on the PrizmDoc Server instance, but also because creation of Viewing Packages for larger documents will not complete immediately.

Uploading a Document Workfile

Upload a document file to PrizmDoc Server using fetch:

const fetch = require('node-fetch');
const fs = require('fs/promises');

const prizmDocHost = 'https://prizmdoc.my.domain';

const body = await fs.readFile('/path/to/file', { flag: 'r' });
let res = await fetch(`${prizmDocHost}/PCCIS/V1/WorkFile`, {
  method: 'POST',
  body
});

const { fileId, affinityToken } = await res.json();

Where fileId is the unique identifier for the file that was uploaded and affinityToken is an identifier for the server within the cluster that handled the request. Subsequent requests will include the affinityToken as a header to indicate which server within the cluster the request should be routed. Note that if the PrizmDoc Server cluster only has a single instance, no affinityToken will be returned.

Creating a Viewing Package

Now that the server has the document, a Viewing Package can be created for that document. Creating the Viewing Package is a two-step process: first, a request is made for the package to be created, and second, a request is made periodically to determine if the creation has completed:

// Create a unique packageId
const packageId = Math.random().toString(36).slice(2);
// Start the Viewing Package creation process
res = await fetch(`${prizmDocHost}/v3/viewingPackageCreators`, {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
    'Accusoft-Affinity-Token': affinityToken
  },
  body: JSON.stringify({
    input: {
      packageId,
      source: {
        type: 'workFile',
        fileId
      },
      lifetimeAfterLastUse: '30d'
    }
  })
});
const { processId } = await res.json();

Once the processId is available, the creation process can be periodically polled to determine when it has completed:

while (true) {
  const poll = await fetch(`${prizmDocHost}/v3/viewingPackageCreators/${processId}`, {
    method: 'GET',
    headers: {
      'Accusoft-Affinity-Token': affinityToken
    }
  });
  const { state } = await poll.json();
  if (state === 'complete') {
    break;
  }
}

When the creation has completed, a Viewing Session can be created for the Viewing Package:

const session = await fetch(`${prizmDocHost}/v3/viewingSessions`, {
  method: 'POST',
  headers: {
    'content-type': 'application/json'
  },
  body: JSON.stringify({
    input: {
      source: {
        type: 'viewingPackage',
        packageId
      }
    }
  })
});
const { viewingSessionId } = await session.json();

With the viewingSessionId available, the PDF Viewer can be created by setting the sourceDocument with a ViewingSession object containing the documentId (viewingSessionId) and the proxied imageHandlerUrl.

If the proxy was configured following How To Configure a Reverse Proxy for PrizmDoc Server, then the imageHandlerUrl will be simply:

const imageHandlerUrl = `${prizmDocHost}/prizmdoc`;

The PDF Viewer instance is created using the snippet below:

const { PdfViewerControl } = require('@accusoft/pdf-viewer');

...

(async () => {
  window.myViewer = await PdfViewerControl.create({
    container: document.getElementById('myContainer'),
    sourceDocument: {
      documentId: viewingSessionId,
      imageHandlerUrl
    },
    licenseKey: 'YOUR_LICENSE_KEY', // or 'eval'
  });
})();

Related

© 2022 Accusoft Corporation. All rights reserved.