Home > front end >  how to convert to application/pdf response to file in axios?
how to convert to application/pdf response to file in axios?

Time:11-04

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

CodePudding user response:

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'));

CodePudding user response:

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

  • Related