How to read pdf stream in angularjs

后端 未结 5 440
[愿得一人]
[愿得一人] 2020-12-01 06:32

I got the following PDF stream from a server: \"PDF

How can this stream be read in AngularJS? I tried

相关标签:
5条回答
  • 2020-12-01 06:57

    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.

    0 讨论(0)
  • 2020-12-01 07:07

    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();
    
    0 讨论(0)
  • 2020-12-01 07:09

    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.

    0 讨论(0)
  • 2020-12-01 07:10

    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);
        });
    
    0 讨论(0)
  • 2020-12-01 07:12

    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);
        });
    
    0 讨论(0)
提交回复
热议问题