Java 如何在angularjs中读取pdf流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25781927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:07:27  来源:igfitidea点击:

How to read pdf stream in angularjs

javaangularjspdfinputstream

提问by User4567

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

我从服务器获得了以下 PDF 流: PDF流

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:

如何在 AngularJS 中读取这个流?我尝试使用以下代码在新窗口中将其作为 PDF 文件打开:

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

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

但是我无法在打开的窗口中看到内容。

采纳答案by User4567

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

回答by Lee Willis

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.

看看PDF.JS。这是一个客户端 javascript 库,可以获取 pdf 流并将其呈现在客户端。Angular 无法读取 pdf,因此这不是角度问题。

回答by Gurupreet

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:

在 HTML 端:

<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-pdfviewerand view your pdf by using it's js files.

您也可以使用 Angular ng-pdfviewer并使用它的 js 文件查看您的 pdf。

回答by Vinay Adki

Java: Get Method

Java:获取方法

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

回答by Ed Greaves

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:

使用 config.responseType 的问题在于 $http 服务仍然运行默认的 responseTransformer 并尝试将响应转换为 JSON。此外,您正在发送默认的接受标头。这是一个(未经测试的)替代方案:

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