30

I got the following PDF stream from a server: PDF STREAM

How can this stream be read in AngularJS? I tried to open this as a PDF file in a new window by using the following code:

.success(function(data) {
   window.open("data:application/pdf," + escape(data));
 });

But I'm unable to see the content in opened window.

4
  • 1
    why do you transfer the whole file through http and not just open the url of that file? window.open(fileUrl)? Commented Sep 11, 2014 at 8:27
  • @MajoB I tried in that way also but in angular I'm getting error like Not allowed to load local resource: failed to load file path.. Commented Sep 11, 2014 at 8:51
  • 1
    See this answer : stackoverflow.com/questions/21628378/… Commented Sep 11, 2014 at 9:04
  • Hi I am getting the same kind of response in Angular2, but i can't figure out how to handle such response in angular 2. I am getting an error "SyntaxError: Unexpected token % in JSON at position 0 at JSON.parse (<anonymous>) at Function.Json.parse" like this. Commented Nov 28, 2016 at 9:19

5 Answers 5

51

I achieved this by changing my controller code

$http.get('/retrievePDFFiles', {responseType: 'arraybuffer'})
       .success(function (data) {
           var file = new Blob([data], {type: 'application/pdf'});
           var fileURL = URL.createObjectURL(file);
           window.open(fileURL);
    });
Sign up to request clarification or add additional context in comments.

7 Comments

Ranjit, I was getting blank pdf but adding the "responseType" to the arguments of the $http, it works. I just figured it out, sorry about being late.
hi I am getting a similar error as Ranjit. On chrome it says: Failed to load PDF document do you know how to solve this?
I am still getting the same error, even after applying the fix.
@RanjitSingh see this ans And replace $window.open($sce.trustAsResourceUrl(fileURL));
how to trigger download dialog box instead of opening it in new tab?
|
7

Pleas have a look on the following code:

On Controller Side -

$http.get(baseUrl + apiUrl, { responseType: 'arraybuffer' })
          .success(function (response) {                  
             var file = new Blob([response], { type: 'application/pdf' });
             var fileURL = URL.createObjectURL(file);
             $scope.pdfContent = $sce.trustAsResourceUrl(fileURL);
           })
           .error(function () {                        
           });

On HTML Side:

<div ng-controller="PDFController" class="modal fade" id="pdfModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
    <div class="modal-content" onloadstart="">
        <object data="{{pdfContent}}"  type="application/pdf" style="width:100%; height:1000px" />
    </div>
</div>

Also you can go with the Angular ng-pdfviewer and view your pdf by using it's js files.

Comments

1

Have a look at PDF.JS. This is a client side javascript library that can fetch a pdf stream and render it client side. Angular is unable to read a pdf so this isn't an angular issue.

Comments

1

Java: Get Method

BLOB pdfData = getBlob_Data;
response.setContentType(pdfData.getContentType());
response.setHeader(ApplicationLiterals.HEADER_KEY_CONTENT, "attachment;     filename=FileName.pdf");
response.getOutputStream().write(pdfData.getBinaryData());
response.getOutputStream().flush();

1 Comment

Controller : function downloadPDF(searchManagerQuery) { // calls get method $window.open('ServiceURL', '_blank'); }
1

The problem with using config.responseType is that the $http service still runs the default responseTransformer and attempts to convert the response to JSON. Also, you are sending the default accept headers. Here is an (untested) alternative:

$http.get('/retrievePDFFiles', {
    headers: { Accept: "application/pdf"},  
    transformResponse: function(data) {
        return new Blob([data], {type: 'application/pdf'});
    }}).success(function (data) {
           var fileURL = URL.createObjectURL(data);
           window.open(fileURL);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.