What is the best location to call PDFNet APIs when used in conjunction with WebViewer?

Q: What is the best location to call PDFNet APIs when used in conjunction with WebViewer?

A: The recommended place to use PDFNet APIs is from the instance.Core object that is resolved from the WebViewer constructor function:

WebViewer({
  path: '/lib',
  initialDoc: 'myfile.pdf',
  fullAPI: true
}).then(instance => {
  const { PDFNet } = instance.Core;

  // call PDFNet APIs here
});

Note that this is recommended because using including PDFNet.js in the same frame as the WebViewer object can easily lead to undesirable behaviour. In particular it is very easy to accidentally create two PDFNetJS worker backends: one when calling PDFNet.initialize() in the context where WebViewer is created (the parent frame) and one in WebViewer’s internal iframe. (the child frame) This can lead to an unfortunate set of issues because:

  1. Multiple workers will consume more memory and may lead to memory exhaustion on some browsers.
  2. Documents created in one context cannot be used in the other context. (so getting the document from WebViewer’s internal iframe and trying to use it in the outer iframe would lead to an error)

Using the PDFNet APIs from instance.Core will avoid these issues because it is using PDFNet that is loaded in WebViewer’s internal iframe.