如何在angularjs中阅读pdf流

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

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

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

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

我通过更改控制器代码实现了这一点

 $http.get('/retrievePDFFiles', {responseType: 'arraybuffer'}) .success(function (data) { var file = new Blob([data], {type: 'application/pdf'}); var fileURL = URL.createObjectURL(file); window.open(fileURL); }); 

请看以下代码:

在控制器端 –

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

在HTML方面:

  

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

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

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

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