2017-05-19 132 views
0

我想使用this在我的SPA中显示PDF。angular-pdfjs-viewer无法正常工作

我试图建立一个例子,但它没有奏效。看看这个Plunker

http://plnkr.co/edit/nZAjYr?p=preview

在这个例子中我的PDF是http://example.com/file.pdf

我真的不知道哪里是错误的,也许这是我CONTROLER:

angular.module('app', ['pdfjsViewer']); 

angular.module('app').controller('AppCtrl', function ($scope, $http, $timeout) { 
    var url = 'http://example.com/file.pdf'; 

    $scope.pdf = { 
     src: url, // get pdf source from a URL that points to a pdf 
     data: null // get pdf source from raw data of a pdf 
    }; 

    getPdfAsArrayBuffer(url).then(function (response) { 
     $scope.pdf.data = new Uint8Array(response.data); 
    }, function (err) { 
     console.log('failed to get pdf as binary:', err); 
    }); 

    function getPdfAsArrayBuffer (url) { 
     return $http.get(url, { 
      responseType: 'arraybuffer', 
      headers: { 
       'foo': 'bar' 
      } 
     }); 
    } 
}); 

灿请人帮助我吗?

回答

1

你的控制器应是这个样子:

angular.module('app').controller('AppCtrl', function ($scope, $sce, $http, $timeout) { 
    var url = 'http://example.com/file.pdf'; 
    PDFJS.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js' 

    $scope.pdf = { 
     src: $sce.trustAsResourceUrl(url), // get pdf source from a URL that points to a pdf 
     data: null // get pdf source from raw data of a pdf 
    }; 
}); 

删除不必要的代码,并检查控制台了解更多详情。