kfm
kfm

Reputation: 143

angular-pdfjs-viewer is not working

I would like to use this to show PDF in my SPA.

I tried to build a single example but it didn't work. Look this Plunker

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

In this example my pdf is in http://example.com/file.pdf

I really don't know where is the error, maybe it be in my 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'
            }
        });
    }
});

Can anyone helpe me please?

Upvotes: 0

Views: 2672

Answers (1)

Beslinda N.
Beslinda N.

Reputation: 5326

Your controller should look something like this:

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

Remove the unnecessary code and check the console for more details.

Upvotes: 1

Related Questions