警告:无效流:dropzone 上传后来自 springboot API 的 PDF 响应上出现“FormatError: Bad FCHECK in flate flow: 120, 239”

Phi*_*ely 2 pdf angularjs dropzone.js angular

我正在使用 dropzone 将文件发送到 springboot api,该 api 处理这些文件并返回 pdf 响应,我想将其显示在我的角度前端上。一切正常,直到显示 pdf 为止,当它呈现一个空白 pdf(具有正确的页数)以及控制台中的警告时:

\n\n
Warning: Indexing all PDF objects pdf.worker.min.js:1\nWarning: Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 239"\n
Run Code Online (Sandbox Code Playgroud)\n\n

我知道 pdf 本身没有损坏,因为当我向 API 发出 get 请求以检索同一 pdf 的存储版本时,它没有损坏并且呈现良好。

\n\n

我尝试过使用 TextEncoder:

\n\n
let enc = new TextEncoder();\nthis.masterPdf = enc.encode(event[1])\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用文件读取器:

\n\n
  var\xc2\xa0reader\xc2\xa0=\xc2\xa0new\xc2\xa0FileReader();\n\xc2\xa0\xc2\xa0reader.onload\xc2\xa0=\xc2\xa0function()\xc2\xa0{\n      var\xc2\xa0arrayBuffer\xc2\xa0=\xc2\xa0this.result;\n      self.masterPdf =\xc2\xa0new\xc2\xa0Uint8Array(arrayBuffer);\n      console.log(self.masterPdf);\n      self.masterShow = true;\n  }\n  var blob = new Blob([event[1]], {type:\'application/pdf\'});\n  reader.readAsArrayBuffer(blob);\n
Run Code Online (Sandbox Code Playgroud)\n\n

并使用 StringToBytes:

\n\n
let strToByteArr = stringToBytes(event[1]);\nthis.masterPdf = new Uint8Array(strToByteArr);\n
Run Code Online (Sandbox Code Playgroud)\n\n

所有这些情况下的事件都是响应,如下所示(当然 pdf 字符串实际上要长得多):

\n\n
0: File(1237) {upload: {\xe2\x80\xa6}, status: "success", accepted: true, processing: true, xhr: XMLHttpRequest, \xe2\x80\xa6}\n1:"%PDF-1.7\xe2\x86\xb5%\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xe2\x86\xb51 0 obj\xe2\x86\xb5<</Type/Catalog/Pages 2 0 R/Lang(en-US) /StructTreeRoot 12 0 R/MarkInfo<</Marked true>>/Metadata 32 0 R/ViewerPreferences 33 0 R>>\xe2\x86\xb5endobj\xe2\x86\xb52 0 obj\xe2\x86\xb5<</Type/Pages/Count 2/Kids[ 3 0 R 9 0 R] >>\xe2\x86\xb5endobj\xe2\x86\xb53 0 obj\xe2\x86\xb5<</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>>\xe2\x86\xb5endobj\xe2\x86\xb54 0 obj\xe2\x86\xb5<</Filter/FlateDecode/Length 3808>>\xe2\x86\xb5stream\n2: ProgressEvent {isTrusted: true, lengthComputable: true, loaded: 99282, total: 99282, type: "load", \xe2\x80\xa6}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我确实检查了返回的有效文件中的那些特定字节(120-239)与无效文件,它们是不同的,但无效文件看起来就像正常数字。

\n

dep*_*ion 5

从 API 请求 PDF 时,请将请求的响应类型设置为“blob”。默认类型是 application/json。

收到文件后,使用响应的数据创建一个 blob,然后使用该 blob 创建一个 url。

  let blob = new Blob([data], {type: 'application/pdf'})
  let url = window.URL.createObjectURL(blob)
Run Code Online (Sandbox Code Playgroud)