如何使用JavaScript显示从S3获取的PDF?
我需要从s3.amazonaws.com获取PDF文件,并且当我使用Postman(或直接粘贴到浏览器中)查询时,它可以很好地加载。但是,当我尝试生成其文件路径(以后传递给观众)时,它不起作用:
fetch(<S3URL>).then(res => res.blob()).then(blob => {
// THIS STEP DOES NOT WORK
let myBlob = new Blob(blob, {type: 'application/pdf'});
// expect something like 'www.mysite.com/my-file.pdf'
let PDFLink = window.URL.createObjectURL(myBlob);
return PDFLink;
}
我正在使用Autodesk的Forge PDF查看器,并且它对本地PDF文件的工作正常很好:
let myPDFLink = 'public/my-file.pdf';
Autodesk.Viewing.Initializer(options, () => {
viewer = new Autodesk.Viewing.Private.GuiViewer3D(document.getElementById('forgeViewer'));
viewer.start();
viewer.loadExtension('Autodesk.PDF').then( () => {
viewer.loadModel(myPDFLink, viewer); // <-- works fine here
});
});
// from https://github.com/wallabyway/offline-pdf-markup
因此,我如何去从S3 URL(例如 s3.amazonaws.com/com.autodesk.oss-persistent/0d/ff/c4/c4/2dfd1860d1 ...
)到PDF查看者可以理解的东西(即具有 .pdf
在URL中扩展)?
我知道对于JSON文件,我需要执行 res.json()
提取JSON内容,但是对于PDF,我该如何处理 res
对象?
注意:我无法控制S3 URL。每当我想从其BIM360门户下载文档时,Autodesk就会生成临时S3链接。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了很多选项,并且可以通过API调用来显示PDF 的唯一方法是使用对象元素:
将下载的blob转换为base64不起作用。将PDF链接放入iFrame中也无法正常工作(它仍然下载而不是显示)。我读过的所有选项仅在PDF是前端应用程序的一部分时(即本地文件,而不是从远程服务器获取的内容)。
I tried a lot of options and the only way I could display a PDF fetched via API calls is by using an object element:
Converting the downloaded blob to base64 doesn't work. Putting the PDF link in an iframe doesn't work either (it still downloads instead of displaying). All the options I have read only work if the PDFs are part of the frontend application (i.e. local files, not something fetched from a remote server).