Search code examples
javascriptautodesk-forgeautodesk-viewerforge

Autodesk Forge: Loading PDF does not trigger onItemLoadSuccess


We have an application running that currently works with both 3D and 2D files, and do not experience any issues when loading 3D files and DWG.

But when trying to load a PDF neither my "onItemLoadSuccess" or "onItemLoadFail" gets run

Autodesk.Viewing.Initializer(options, function onInitialized() {

    // Select the container for the viewer
    viewerApp = new Autodesk.Viewing.ViewingApplication(container);

    // Load settings, i.e extension manager
    viewerApp.registerViewer(viewerApp.k3D, 
Autodesk.Viewing.Private.GuiViewer3D, { extensions: [ 'ExtensionManager'] });

    // Select model to load defined by URN 
    viewerApp.loadDocument(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
  });
}

function onDocumentLoadSuccess(doc) {
  var viewables = viewerApp.bubble.search({ 'type': 'geometry' });
  if (viewables.length === 0) {
    console.error('Document contains no viewables.');
    return;
  }

  // Choose any of the avialble viewables
  viewerApp.selectItem(viewables[0], onItemLoadSuccess, onItemLoadFail);
}

function onItemLoadSuccess(viewer, item) {
  console.log('onItemLoadSuccess()!');
}

function onItemLoadFail(errorCode) {
  console.error('onItemLoadFail() - errorCode:' + errorCode);
}

The PDF file will still open and load, so I am wondering if there might be a different way to run an onItemLoadSuccess function, or we have to do something a bit differently to ensure that our PDF's also gets loaded correctly.

Any help is highly appreciated!


Solution

  • Starting from Viewer v6.3 you can load PDF directly with Autodesk.PDF and pass in callbacks to loadModel like you do other models:

      Autodesk.Viewing.Initializer(options, function() {
        viewer.start()
        viewer.loadExtension('Autodesk.PDF').then(function() {
            viewer.loadModel('/path/to/pdf', { page: 1 }, onLoadSuccess, onLoadFail);
        });
      });
    

    See release notes here: https://forge.autodesk.com/blog/viewer-release-notes-v-63