问题
I want to use the vue-pdf library that is an implementation of pdfjs for vuejs 2.x in order to do the following
- Download a PDF from an oauth2 protected endpoint using axios
- Render the PDF (octet-stream) using vue-pdf library
and the tricky parts are
- Access protected resources
- Render PDF that comes as octet stream
Currently there are no examples in the repo to show case these.
回答1:
After fiddling around with the library, I managed to do achieve the rendering of a pdf from a protected endpoint using the following approach
- Make an axios ajax request for the protected resource using necessary auth header and the response type as
responseType: 'blob'
- Magically create a URL from the downloaded blob object
- Set the blob URL in a data variable that is then used by
<pdf>
component
I have created a Pull Request to the vue-pdf repository with a working example. In the PR replace the URL of axios
request with a REST endpoint that returns an octet-stream
and you should be all good.
The resulting pdf viewer shown below
来源:https://stackoverflow.com/questions/49833420/how-to-consume-an-octet-stream-from-protected-endpoint-using-vue-pdf-a-vuejs-imp