如何使用JavaScript显示从S3获取的PDF?

发布于 2025-01-30 16:52:11 字数 1318 浏览 2 评论 0 原文

我需要从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链接。

I need to fetch a PDF file from s3.amazonaws.com and when I query it using Postman (or paste directly into the browser), it loads fine. However when I try to generate the file path for it (to pass to a viewer later), it didn't work:

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;        
}

I'm using Autodesk's Forge PDF viewer and it works perfectly fine for local PDF files:

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

So, how do I go from the S3 URL (e.g. s3.amazonaws.com/com.autodesk.oss-persistent/0d/ff/c4/2dfd1860d1...) to something the PDF viewer can understand (i.e. has .pdf extension in the URL)?

I know for JSON files I need to do res.json() to extract the JSON content, but for PDFs, what should I do with the res object?

Note: I don't have control over the S3 URL. Autodesk generates a temporary S3 link whenever I want to download documents from their BIM360 portal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2025-02-06 16:52:11

我尝试了很多选项,并且可以通过API调用来显示PDF 的唯一方法是使用对象元素:

<object data='<PDF link>' type='application/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:

<object data='<PDF link>' type='application/pdf'>

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文