简体   繁体   English

如何显示从 api 返回的 PDF?

[英]How do I display a PDF that is getting returned from an api?

I'm working on a JavaScript/React project and I have a GET endpoint that when I hit returns back a PDF.我正在处理一个 JavaScript/React 项目,我有一个 GET 端点,当我点击返回一个 PDF 时。 I am able to hit the endpoint successfully and am getting back a 200 success back but the response I'm getting back looks like something along the lines of....我能够成功到达终点,并成功恢复了 200,但我得到的响应看起来像......

%PDF-1.4 1 0 obj << /Title ( ) /Creator( ) /Producer( ).... %PDF-1.4 1 0 obj << /Title ( ) /Creator( ) /Producer( )....

Does anyone know how I can take this and turn it into the PDF file in a separate window or popup?有谁知道我如何将其转换为单独的窗口或弹出窗口中的 PDF 文件? All the solutions I have found are requiring me to have a pdf url or file to do this.我找到的所有解决方案都要求我有一个 pdf url 或文件来执行此操作。 I'm completely new to this and would really appreciate some help.我对此完全陌生,非常感谢一些帮助。

If you set the return content type to "application/pdf", your browser should display it correctly I think.如果您将返回内容类型设置为“application/pdf”,我认为您的浏览器应该正确显示它。 How you set the content type depends on what you're using server side.如何设置内容类型取决于您使用的服务器端。

If you also develop the API, setting Content-Disposition header and Content-Type hader , it will be perfect.如果你也开发API,设置Content-Disposition headerContent-Type hader就完美了。

Also do not use a button that call a fetch to the url, just use a link, a real not the react router Link也不要使用调用 fetch 到 url 的按钮,只使用一个链接,一个真正的不是反应路由器链接

我认为您应该尝试从端点以 base64 URI 的形式发送您的 PDF 文件,以便您可以下载文件或在浏览器中打开它,您的 API 应该响应以下链接:

echo "data:application/pdf;base64,".base64_encode($YourFileHere);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM