API returns PDF二进制流时如何处理

How to handle when API returns PDF binary stream

我有一个正在使用的电子签名工具,当我想下载执行的文件时,这是一个简单的 GET 请求。问题是它:

Returns PDF file binary stream of the document which you can save as PDF file.

检查文件时,它看起来像这样(这只是一个片段):

> r.body
 => "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n77 0 obj\n<</ByteRange [0 141 16527 447870 ]                                                           /ContactInfo()/Contents <30820ffe06092a864886f70d010702a0820fef30820feb020101310f300d06096086480165030402010500300b06092a864886f70d010701a0820e393082042a30820312a00302010202043863def8300d06092a864886f70d01010505003081b431143012060355040a130b456e74727573742e6e65743140303e060355040b14377777772e656e74727573742e6e65742f4350535f3230343820696e636f72702e206279207265662e20286c696d697473206c6961622e2931253023060355040b131c286329203139393920456e74727573742e6e6574204c696d69746564313330310603550403132a456e74727573742e6e6574204365727469666963

我可以如何处理这些数据以将其作为 PDF 显示在屏幕上?

您可以使用 send_data,这将下载文件或在浏览器中打开文件,具体取决于您的用户使用的浏览器:

send_data(r.body, type: 'application/pdf', disposition: 'attachment', filename: 'file.pdf')

进一步了解:https://apidock.com/rails/ActionController/Streaming/send_data