从 blob React-Native 打开 PDF (expo)

wal*_*tis 7 pdf api blob react-native expo

我是新来的,我正在使用react-native和expo制作我的第一个应用程序,并且我遇到了来自API的PDF响应的问题。

这里的目标是能够调用 API 并接收应用程序/pdf 响应,并在按下按钮时下载它。所有这些工作都很顺利,但我无法设法解释 pdf 响应并从中执行某些操作。

我尝试过的一些方法:

  • 在 pdfViewer 中解释它
  • 使用 Expo 文件系统下载
  • 在新窗口或 webView 中打开它
  • 使用 rn-fetch-blob (因为世博会而不能)
  • 使用 Print.printToFileAsync
  • ...

大多数问题来自这样一个事实:我需要文件中的 uri 来对其执行任何操作,但看起来我无法从 Blob 创建 url。当我尝试 createObjectURL 时出现此错误

错误:无法为 blob 创建 URL!

// Working API Call
  response = await axios.get(apiUri.DownloadDocument +id) this.props.id);

// Blob creation
  const file = new Blob(
    [response.data],
    {type: 'application/pdf'});

// This line throws an error
  const fileURL = URL.createObjectURL(file);
  WebBrowser.openBrowserAsync(fileURL);
Run Code Online (Sandbox Code Playgroud)

如果我用 POSTMAN 调用 API,他无法预览响应,但我可以下载它,并且它是 pdf 文件,没有问题。

此问题可能与62387371 59622283 55507959有关

因为我对此很陌生,所以我可能会以完全错误的方式做这件事。感谢您的时间和专业知识。这是我通过 console.log(response.data) 获得的 API 响应的输出

o << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >>
/FontDescriptor 28 0 R
/CIDToGIDMap /Identity
/W [0 [595 606 629 610 608 389 408 610 315 856 608 276 517 607 629 630 276 693 629 631 631 315 631 496 571 293 742 679 553 679 781 627 598 764 334 545 349 630 630 631 631 358 631 631 631 334 966 631 992 315 587 587 630 681 630 ]
]
>>
endobj
31 0 obj
<< /Length 742 >>
stream
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
2 beginbfrange
<0000> <0000> <0000>
<0001> <0036> [<0054> <0068> <00E9> <00E2> <0074> <0072> <0065> <0020> <004D> <0061> <0069> <0073> <006F> <006E> <0064> <006C> <0043> <0075> <0031> <0035> <002C> <0030> <005F> <0046> <0049> <004E> <0041> <004C> <0056> <004F> <0045> <0050> <0044> <003A> <0063> <0066> <0062> <0067> <0032> <0039> <002D> <0038> <0033> <0036> <002F> <006D> <0037> <0040> <002E> <0076> <0078> <0070> <0042> <0071> ]
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end
end

endstream
endobj
7 0 obj
<< /Type /Font
/Subtype /Type0
/BaseFont /DejaVuSans
/Encoding /Identity-H
/DescendantFonts [30 0 R]
/ToUnicode 31 0 R>>
endobj
2 0 obj
<<
/Type /Pages
/Kids 
[
5 0 R
]
/Count 1
/ProcSet [/PDF /Text /ImageB /ImageC]
>>
endobj
xref
0 33
0000000000 65535 f 
0000000009 00000 n 
0000025854 00000 n 
0000000157 00000 n 
0000000252 00000 n 
0000000690 00000 n 
0000015892 00000 n 
0000005735 00000 n 
0000006000 00000 n 
0000014618 00000 n 
0000015070 00000 n 
0000014597 00000 n 
0000016033 00000 n 
0000016293 00000 n 
0000024494 00000 n 
0000024924 00000 n 
trailer
<<
/Size 33
/Info 1 0 R
/Root 13 0 R
>>
startxref
25952
%%EOF
Run Code Online (Sandbox Code Playgroud)

小智 1

在这个线程中有一个答案:

我发现使用 Expo 处理 PDF 文件的最佳方法是使用缓冲区将它们转换为 Base64 文件,将它们存储在文件系统中并共享它们:

/sf/answers/5198512301/