简体   繁体   English

使用PDF.js查看器显示受保护资源提供的pdf

[英]Using PDF.js viewer to display a pdf served by a protected resource

I'm investigating how to use the pdfjs viewer to serve a PDF that is behind a protected resource. 我正在研究如何使用pdfjs查看器来提供受保护资源背后的PDF。

From my understanding, this would work if the resource allowed anonymous access: https://app.com/pdf.js/web/viewer.html?file=https://app.com/pdf/{id} 根据我的理解,如果资源允许匿名访问,这将起作用: https://app.com/pdf.js/web/viewer.html?file=https://app.com/pdf/{id}https://app.com/pdf.js/web/viewer.html?file=https://app.com/pdf/{id}

The resource https://app.com/pdf/{id} returns a response with content type application/pdf . 资源https://app.com/pdf/{id}返回内容类型为application/pdf的响应。

However, that resource requres a OAuth2 token to be present in the authorization header. 但是,该资源要求OAuth2令牌出现在授权标头中。 So is it possible to modify the headers created by the viewer, to include a authorization header and pass the token of the user? 那么是否可以修改查看器创建的标头,包括授权标头并传递用户的标记?

PDF.js can read file in Base64 format ( example ). PDF.js可以读取Base64格式的文件( 示例 )。 So You can use Ajax / HTTP Client to download binary data with authorization header, convert to Base64 string then embed into PDF 因此,您可以使用Ajax / HTTP Client下载具有授权标头的二进制数据,转换为Base64字符串,然后嵌入到PDF中

Edit: You can set HTTP headers to PDF getDocument function. 编辑:您可以将HTTP标头设置为PDF getDocument函数。 So you can store access token in Web Storage, then get it in pdf viewer's page 因此,您可以在Web Storage中存储访问令牌,然后在pdf viewer的页面中获取它

var loadingTask = pdfjsLib.getDocument({
  url,
  withCredentials,
  httpHeaders: {
    authentication: "abcxyz",
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM