Monday, 3 September 2018

Angular JS with PDFTron

I am trying to get a blob as a URL but i get an error on this line :

xhr.send()

The error message is angular.js:13920 Error: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.

But in the code I am using xhr.open('GET', blobValue) as shown in my code here

   //check new value to be what you expect.       
                if(newValue){
                    var viewerElement = $document[0].getElementById('viewer');
                    $window.myWebViewer = new $window.PDFTron.WebViewer({
                        path: 'lib',
                        pdftronServer: 'https://demo.pdftron.com' // remove
                    }, viewerElement);

                    var xhr = new XMLHttpRequest;
                    xhr.responseType = 'blob';

                    xhr.onload = function() {
                        var recoveredBlob = xhr.response;

                        var reader = new FileReader;

                        // reader.onload = function() {
                        //     var blobAsDataUrl = reader.result;
                        //     $window.location = blobAsDataUrl;
                        // };

                        reader.readAsDataURL(recoveredBlob);
                    };

                    xhr.open('GET', newValue);
                    xhr.setRequestHeader('Content-type', 'application/pdf');
                    xhr.send(); //error here although its open?
                    //var file = new File([newValue], 'somefile.pdf');
                    $window.myWebViewer.loadDocument(xhr.response, { filename: 'somefile.pdf'});

Currently i have the document as a blob but i am trying to load it to pdftron library and unfortunately i dont seem to find the myWebViewer.getInstance().loadLocalFile method in the DOM (its undefined).

Would appreciate any pointers as its the first time trying to use pdftron in the angularjs app.

NOTE : This is inside a directive.



from Angular JS with PDFTron

No comments:

Post a Comment