简体   繁体   English

如何将远程服务中的 PDF 嵌入 html 页面?

[英]How to embed PDF from remote service into html page?

I'm developing a UI that interfaces with an existing back-end service.我正在开发一个与现有后端服务交互的 UI。 The UI needs to make a call to the back-end server for a PDF file that will either be displayed on the existing page or in a new tab. UI 需要调用后端服务器以获取 PDF 文件,该文件将显示在现有页面或新选项卡中。

I've tried all the options I've seen listed on SO:我已经尝试了我在 SO 上看到的所有选项:

<iframe src="http://localhost:3131/Reports.aspx?reportName=testReport&clientid=23" width="800px" height="100%"></iframe>

<embed type="application/pdf" src="//http://samelinkasabove" width="800px" height="100%">

<object type="application/pdf" data="http://myreportlink" width="800px" height="100%" />

<a href="http://localhost:32/Reports.aspx?reportName=testReport&clientid=23" target="_blank">View Report</a>

In every case, the pdf winds up as a download rather than being displayed in a browser window.在任何情况下,pdf 都会作为下载结束,而不是显示在浏览器 window 中。 Is there a native way to display a pdf or is javascript required to make this happen?是否有一种本地方式来显示 pdf 或者是否需要 javascript 才能实现这一点?

I have a CodePen here that shows exactly this functionality using the free Adobe DC View SDK .这里有一个 CodePen ,它使用免费的Adobe DC View SDK准确显示了这个功能。 You can't control the PDF experience unless you override the native browser viewer.除非您覆盖本机浏览器查看器,否则您无法控制 PDF 体验。 The relevant code for my example is below.我的示例的相关代码如下。 In my example, the PDF is fetched from another domain but the "content" parameter will accept any Promise that resolves to an ArrayBuffer for the PDF content.在我的示例中,PDF 是从另一个域获取的,但“内容”参数将接受任何解析为 PDF 内容的 ArrayBuffer 的 Promise。 Your PDF can be stored anywhere or created on the fly and then displayed within the HTML page.您的 PDF 可以存储在任何地方或动态创建,然后显示在 HTML 页面中。

document.addEventListener("adobe_dc_view_sdk.ready", function () {
  /*
  The clientId is tied to a specific domain. To display a PDF hosted 
  on a different domain using the Adobe View SDK, we need to fetch the file 
  first then pass it to the "content" parameter as a Promise. 
  */
  fetch("https://documentcloud.adobe.com/view-sdk-demo/PDFs/Bodea Brochure.pdf")
    .then((res) => res.blob())
    .then((blob) => {
      var adobeDCView = new AdobeDC.View({
        // This clientId can be used for any CodePen example
        clientId: "YOUR_CLIENT_ID", 
        // The id of the container for the PDF Viewer
        divId: "adobe-dc-view" 
      });
      adobeDCView.previewFile(
        {
          // The file content
          content: { promise: Promise.resolve(blob.arrayBuffer()) },
          metaData: { fileName: "Bodea Brochure.pdf" }
        },
        {
          embedMode: "FULL_WINDOW", // possible values are "FULL_WINDOW", "SIZED_CONTAINER" and "IN_LINE"
          defaultViewMode: "FIT_WIDTH", // possible values are  "FIT_WIDTH" and "FIT_PAGE"
          showDownloadPDF: true,
          showPrintPDF: true,
          showLeftHandPanel: true,
          showAnnotationTools: true
        }
      );
    });
});

You can get your own credentials here您可以在此处获取自己的凭据

I found that the server was using CrystalReports to generate the PDF and "export" it.我发现服务器正在使用 CrystalReports 生成 PDF 并“导出”它。 It used a function ExportToHttpResponse(...) and the third parameter in the method call was bool asAttachment .它使用了 function ExportToHttpResponse(...)并且方法调用中的第三个参数是bool asAttachment

That parameter was being set to true.该参数被设置为true。 I changed it to false and the response began being set to inline and the above display methods began working.我将其更改为 false 并且响应开始设置为inline并且上述显示方法开始工作。

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

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