How to consume an octet stream from protected endpoint using vue-pdf a vuejs implementation of pdfjs

守給你的承諾、 提交于 2020-03-05 03:21:11

问题


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

  1. Make an axios ajax request for the protected resource using necessary auth header and the response type as responseType: 'blob'
  2. Magically create a URL from the downloaded blob object
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!