Can we significantly reduce the memory?

Hi Ricky,

Currently we don’t support splitting of the modules when certain features aren’t being used. This is a longer term thing that we’re looking into but don’t yet have a timeline for when it would be available.

For loading multiple instances of WebViewer on the same page the main thing that you can do to save memory is to share the PDF worker objects across all viewer instances. Here are the steps to do this:

  1. Include a script tag pointing to CoreControls.js directly on your app’s HTML page. CoreControls.js is normally only loaded inside the iframe but in this case we also need to load it on your app’s page so that we can create the workers there.
  2. Use code similar to the following when instantiating your WebViewer instances. Basically you need to create a “worker transport promise” which will be passed to each WebViewer instance. This will allow each instance to share the same worker. The worker path may need to be adjusted as it will be relative to your HTML file. In this example I modified the viewing sample included in the WebViewer download package but you may need to adjust the path.
CoreControls.setWorkerPath('../../../lib/core');

CoreControls.getDefaultBackendType().then(function(pdfType) {
  var workerHandlers = {};
  var licenseKey = 'LICENSE_KEY_HERE';

  var workerTransportPromise = CoreControls.initPDFWorkerTransports(pdfType, workerHandlers, licenseKey);
  // Instantiate
  var viewerElement = document.getElementById('viewer');
  var viewer = new PDFTron.WebViewer({
    path: '../../../lib',
    l: licenseKey,
    initialDoc: '[https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo-annotated.pdf](https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo-annotated.pdf)',
    workerTransportPromise: {
      pdf: workerTransportPromise
    }
  }, viewerElement);

  var viewerElement2 = document.getElementById('viewer2');
  var viewer2 = new PDFTron.WebViewer({
    path: '../../../lib',
    l: licenseKey,
    initialDoc: '[https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo-annotated.pdf](https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo-annotated.pdf)',
    workerTransportPromise: {
      pdf: workerTransportPromise
    }
  }, viewerElement2);
});

Matt Parizeau
Software Developer
PDFTron Systems Inc.