[英]load pdf in javascript error, but why directly in browser successfully
all.全部。 I am thinking about a issue using pdfTron SDK(an SDK help load pdf) in my application.
我正在考虑在我的应用程序中使用 pdfTron SDK(SDK 帮助加载 pdf)的问题。 This SDK is able to load local pdf files, working like a charm.
这个 SDK 能够加载本地 pdf 文件,工作起来很有魅力。 I decide to load an online pdf file in my web application, using pdfTron SDK, eg 'https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf'.
我决定在我的 web 应用程序中加载在线 pdf 文件,使用 pdfTron SDK,例如“https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf”。 This file is accessible in browser, directly, but when I use scripts, like:
这个文件可以在浏览器中直接访问,但是当我使用脚本时,比如:
instance.UI.loadDocument('https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf');
};
I directly got an error when running the application, which shows我在运行应用程序的时候直接报错,显示
Request URL: https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf
Request Method: HEAD
Status Code: 403
Referrer Policy: strict-origin-when-cross-origin
I think this is a CORS issue.我认为这是一个 CORS 问题。 But I cannot understand why I can use browser getting this file, while scripts does not.
但我不明白为什么我可以使用浏览器获取此文件,而脚本却不能。 How can I resolve this?
我该如何解决这个问题? Thanks
谢谢
I would suggest to read the documentation on pdfTron concerning "webview" and "loadDocment".我建议阅读 pdfTron 上有关“webview”和“loadDocment”的文档。
WebView is used to get the pdf you want to use from any source(file or web), in your case, you need to use "initialDoc" property, that is where you would place "cdn.mendix.tencent-cloud.com/documentation/developerportal/…". WebView 用于从任何来源(文件或网络)获取您要使用的 pdf,在您的情况下,您需要使用“initialDoc”属性,即放置“cdn.mendix.tencent-cloud.com/文档/开发者门户网站/…”。
Then "instance.UI.loadDocument" is used to identify the html element where you want to place the fetched document.然后“instance.UI.loadDocument”用于标识要放置获取的文档的 html 元素。
I also try a CORS config in my document server(nginx), like this:我还在我的文档服务器 (nginx) 中尝试 CORS 配置,如下所示:
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
This works now.这现在有效。 But It seems that if the document server is not yours, you cannot do this.
但是好像如果文档服务器不是你的,你就不能这样做。 Hope this help!
希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.