2

I'm working on a JavaScript/React project and I have a GET endpoint that when I hit returns back a 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....

%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? All the solutions I have found are requiring me to have a pdf url or file to do this. I'm completely new to this and would really appreciate some help.

navin1551
  • 61
  • 1
  • 9
  • You are forcing the browser to show binary information as text, most probably due to wrong content-type header. See [this answer](https://stackoverflow.com/a/27236017) or use a js plugin like [pdf.js by mozilla](https://mozilla.github.io/pdf.js/)(will work even if a browser doesn't have an inbuit viewer). – AVX-42 Oct 30 '20 at 16:48

3 Answers3

0

If you set the return content type to "application/pdf", your browser should display it correctly I think. How you set the content type depends on what you're using server side.

Cracanut
  • 3
  • 1
  • 2
0

If you also develop the API, setting Content-Disposition header and Content-Type hader , it will be perfect.

Also do not use a button that call a fetch to the url, just use a link, a real not the react router Link

farvilain
  • 2,552
  • 2
  • 13
  • 24
0

I think you should try to send your PDF file from the endpoint as base64 URI so you can Download the file or just open it in your browser, your API should respond something link this :

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