【问题标题】:angular-pdfjs-viewer is not workingangular-pdfjs-viewer 不工作
【发布时间】:2017-06-23 18:15:29
【问题描述】:

我想使用this 在我的 SPA 中显示 PDF。

我尝试构建一个示例,但没有成功。看看这个 Plunker

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

在此示例中,我的 pdf 位于 http://example.com/file.pdf

我真的不知道错误在哪里,可能是在我的控制器中:

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'
            }
        });
    }
});

谁能帮帮我?

【问题讨论】:

    标签: angularjs pdf


    【解决方案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
        }; 
    });
    

    删除不必要的代码并检查控制台以获取更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 2021-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2019-11-12
      • 2021-09-07
      • 1970-01-01
      相关资源
      最近更新 更多