如何使用vue-pdf从受保护的终结点使用八位位组流,pdfjs的vuejs实现 [英] How to consume an octet stream from protected endpoint using vue-pdf a vuejs implementation of pdfjs
问题描述
我想使用 vue-pdf 库,该库是针对vuejs的pdfjs的实现2.x,以便执行以下操作
I want to use the vue-pdf library that is an implementation of pdfjs for vuejs 2.x in order to do the following
- 使用axios从受oauth2保护的端点下载PDF
- 使用vue-pdf库渲染PDF(八位字节流)
棘手的部分是
- 访问受保护的资源
- 呈现八位字节流形式的PDF
当前回购中没有示例来说明这些情况.
Currently there are no examples in the repo to show case these.
推荐答案
摆弄了库之后,我设法使用以下方法从受保护的端点实现了pdf的呈现
After fiddling around with the library, I managed to do achieve the rendering of a pdf from a protected endpoint using the following approach
- 使用必要的auth标头并将响应类型设为
responseType: 'blob'
对受保护的资源发出axios ajax请求
- 神奇地从下载的blob对象创建URL
- 在数据变量中设置Blob URL,然后由
<pdf>
组件 使用
- 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.
生成的pdf查看器,如下所示
The resulting pdf viewer shown below
这篇关于如何使用vue-pdf从受保护的终结点使用八位位组流,pdfjs的vuejs实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!