0

I'm getting response from the API in application/pdf response type. I need to convert this to file or base64 format to show a the file in react-pdf library.

How can I achieve this?

response type

response preview

Any help will much be appreciated

Rajath
  • 2,611
  • 8
  • 27
  • 43

2 Answers2

0

As long as it is a GET request, you can do sth like this:

ar link = document.createElement('a');
link.href = yourGETurl;
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));
k-wasilewski
  • 3,943
  • 4
  • 12
  • 29
0

Well, unless you have some real reason to use Axios for the request, you can do something like that:

<Document
  httpHeaders={headersObjectForAuth}
  file={{ url: sameUrlUsedByAxios }}
>
  <Page {...pageProps} />
</Document>

You can find more info here:

https://github.com/wojtekmaj/react-pdf#props

Emanuele Scarabattoli
  • 4,103
  • 1
  • 12
  • 21