p19 p19 - 4 months ago 107
AngularJS Question

Blob pdf rendering giving error Invalid XRef stream header

I am trying to display a pdf file from my server on my web app through Angular Blob but I am receiving some errors there. I have been stuck with this for too long and tried others things which are not working too. Please suggest.
My Spring controller returns byte[]:

@RequestMapping(value = "/showpdf", method = RequestMethod.POST, produces="application/octet-stream")
@ResponseBody
public byte[] showPDFFile(@RequestBody FilePath filePath, final HttpServletResponse response) throws IOException {
return org.apache.commons.io.FileUtils.readFileToByteArray(file);
}


My angular code:

$http.post('/showpdf',{responseType: 'arrayBuffer'}).success(
function(response) {
console.log(response);
var file = new Blob([response], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
$scope.content = $sce.trustAsResourceUrl(fileURL);

});


My HTML page:

<div>
<object data="{{content}}" type="application/pdf" style="width: 100%; height: 1000px;"></object>
</div>


The pdf window is coming but it does not show any data and keeps on loading in the toolbar(See attached) and the the following error is coming in browser tools.

Error: Invalid XRef stream header
pdf.worker.js (line 250)
<System>

XRef_readXRef@resource://pdf.js/build/pdf.worker.js:3693:13
XRef_parse@resource://pdf.js/build/pdf.worker.js:3289:23
PDFDocument_setup@resource://pdf.js/build/pdf.worker.js:2484:7
PDFDocument_parse@resource://pdf.js/build/pdf.worker.js:2371:7
LocalPdfManager_ensure/<@resource://pdf.js/build/pdf.worker.js:1934:20
LocalPdfManager_ensure@resource://pdf.js/build/pdf.worker.js:1929:1
BasePdfManager_ensureDoc@resource://pdf.js/build/pdf.worker.js:1861:14
loadDocument/</<@resource://pdf.js/build/pdf.worker.js:33208:11

pdf.worker.js (line 252)
<System>
Warning: Unsupported feature "unknown"
pdf.worker.js (line 235)
<System>
Warning: Unsupported feature "unknown"
Warning: Indexing all PDF objects
pdf.worker.js (line 235)
<System>
PDF 0d3cdf6286f2947410f0d270a249f816 [1.4 iText 2.1.3 (by lowagie.com) kSar Version:5.0.6] (PDF.js: 1.0.1149)
Error: Bad uncompressed block length in flate stream
pdf.worker.js (line 250)
<System>

FlateStream_readBlock@resource://pdf.js/build/pdf.worker.js:31244:9
DecodeStream_getByte@resource://pdf.js/build/pdf.worker.js:30882:9
Lexer_nextChar@resource://pdf.js/build/pdf.worker.js:30125:34
Lexer@resource://pdf.js/build/pdf.worker.js:30067:5
EvaluatorPreprocessor@resource://pdf.js/build/pdf.worker.js:12105:30
PartialEvaluator_getOperatorList@resource://pdf.js/build /pdf.worker.js:10616:26
Page_getOperatorList/pageListPromise<@resource://pdf.js/build/pdf.worker.js:2216:16

pdf.worker.js (line 252)
<System>
Warning: Unsupported feature "unknown"
pdf.worker.js (line 235)




I tried hardcoding this pdf, it renders fine.

Answer

Using 'arraybuffer' instead of 'arrayBuffer' as the responseType parameter shall solve the issue:

$http.post('showpdf',{responseType: 'arraybuffer'}).success(
...