我正在使用 dropzone 将文件发送到 springboot api,该 api 处理这些文件并返回 pdf 响应,我想将其显示在我的 Angular 前端上。一切正常,直到显示 pdf 为止,当它呈现一个空白 pdf(具有正确的页数)以及控制台中的警告时:
Warning: Indexing all PDF objects pdf.worker.min.js:1
Warning: Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 239"
我知道 pdf 本身没有损坏,因为当我向 API 发出 get 请求以检索同一 pdf 的存储版本时,它没有损坏并且呈现良好。
我尝试过使用 TextEncoder:
let enc = new TextEncoder();
this.masterPdf = enc.encode(event[1])
使用文件阅读器:
var reader = new FileReader();
reader.onload = function() {
var arrayBuffer = this.result;
self.masterPdf = new Uint8Array(arrayBuffer);
console.log(self.masterPdf);
self.masterShow = true;
}
var blob = new Blob([event[1]], {type:'application/pdf'});
reader.readAsArrayBuffer(blob);
并使用 StringToBytes:
let strToByteArr = stringToBytes(event[1]);
this.masterPdf = new Uint8Array(strToByteArr);
所有这些情况下的事件都是响应,如下所示(当然 pdf 字符串实际上要长得多):
0: File(1237) {upload: {…}, status: "success", accepted: true, processing: true, xhr: XMLHttpRequest, …}
1:"%PDF-1.7↵%����↵1 0 obj↵<</Type/Catalog/Pages 2 0 R/Lang(en-US) /StructTreeRoot 12 0 R/MarkInfo<</Marked true>>/Metadata 32 0 R/ViewerPreferences 33 0 R>>↵endobj↵2 0 obj↵<</Type/Pages/Count 2/Kids[ 3 0 R 9 0 R] >>↵endobj↵3 0 obj↵<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R>>/ExtGState<</GS7 7 0 R/GS8 8 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>↵endobj↵4 0 obj↵<</Filter/FlateDecode/Length 3808>>↵stream
2: ProgressEvent {isTrusted: true, lengthComputable: true, loaded: 99282, total: 99282, type: "load", …}
我确实检查了返回的有效文件与无效文件中的那些特定字节(120-239),它们是不同的,但无效文件看起来就像正常数字。
最佳答案
从 API 请求 PDF 时,请将请求的响应类型设置为“blob”。默认类型是application/json。
收到文件后,使用响应的数据创建一个 blob,然后使用该 blob 创建一个 url。
let blob = new Blob([data], {type: 'application/pdf'})
let url = window.URL.createObjectURL(blob)
关于angularjs - 警告 : Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 239" on PDF response from springboot API after dropzone upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51382667/