首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从在线源将PDF文件嵌入到网页中,这需要下载带有“内容处理:”标签的HTTP-headers文件。

从在线源将PDF文件嵌入到网页中,这需要下载带有“内容处理:”标签的HTTP-headers文件。
EN

Stack Overflow用户
提问于 2013-04-13 18:04:29
回答 1查看 1.9K关注 0票数 2

我需要显示来自第三方的PDF文件在网页上。当文档出现在源页面时,我有指向它们的链接。不幸的是,这些链接中没有一个是指向文档的实际链接,而是具有特定参数的请求或其他间接引用,如:

http://cdm.unfccc.int/UserManagement/FileStorage/SNM7EQ2RUD4IA0JLO3HCZ8BTK1VX5P

如果网站不强制在响应标头中使用Content-Disposition: attachment;标记下载,如上面所示,那么我可以通过以下方式轻松地实现必要的显示:

代码语言:javascript
复制
<object width="90%" height="600" type="application/pdf"  
data="http://cdm.unfccc.int/UserManagement/FileStorage/SNM7EQ2RUD4IA0JLO3HCZ8BTK1VX5P"  
id="pdf_content">  
<p>Can't seem to display the document. Try <a href="http://cdm.unfccc.int/UserManagement/FileStorage/SNM7EQ2RUD4IA0JLO3HCZ8BTK1VX5P">  
downloading</a> it.</p>
<embed type="application/pdf" src="http://cdm.unfccc.int/UserManagement/FileStorage/SNM7EQ2RUD4IA0JLO3HCZ8BTK1VX5P"  
width="90%" height="600" />
</object>

这种“站立”和“跌落”在大多数浏览器中都非常优雅。<object><embed>的使用同时适用于我,就我所测试的情况而言,并不影响我在下面描述的问题(告诉我是否错了)。

当网站确实需要在HTTP头中使用上面提到的标签下载时,问题就开始了。例如,以下链接上的文档:

id=103000000000681

不会通过我上面显示的HTML结构显示。它优雅地落下,下载的链接工作得很好,但我需要查看它!

我已经把头撞在墙上三天了,弄不明白。

也许有一种方法可以以某种方式捕获请求的标题并忽略它们,也可能会强制GET请求中的“查看性”。

对于一般信息,这是Rails应用程序的一部分,因此解决方案应该沿着这些路线来。我在这里没有给任何ROR代码,因为它似乎不是一个关注的来源。

任何直截了当的解决方案都会被祈祷,而任何其他人--非常感激。

我想到并放弃评论的替代解决方案:

  1. 预先将所有这些文件下载到本地存储,然后从那里提供服务。 必要的存储容量将在1TB左右并不断增长,因此将其存储在服务器上将是昂贵的,因为它是一个小型商业SaaS。
  2. 在需要的时候缓存这些文档。例如,当有人打开项目的页面时,后台的流程将下载相关的PDF,因此如果用户单击文档链接,就会得到刚刚下载到本地存储的文档。缓存可以保存几个小时/天,以防用户返回。 这可能是可行的,但如果用户基数很大,则此解决方案将与上面的解决方案相同。在这个时候,我也不知道如何实现这种算法(你看,这是个初学者)。
EN

回答 1

Stack Overflow用户

发布于 2013-07-18 15:07:51

您可能想研究使用http://pdfobject.com,或者可能只是修改它的一些代码,因为它似乎能够做您想做的事情。我想出了一个概念的证明:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Embedding a PDF using PDFObject: Simple example with basic CSS</title>
  <!-- This example created for PDFObject.com by Philip Hutchison (www.pipwerks.com) -->
  <style type="text/css">
    body
    {
      font: small Arial, Helvetica, sans-serif;
      color: #454545;
      background: #F8F8F8;
      margin: 0px;
      padding: 2em;
    }
    h1
    {
      font-weight: normal;
      font-size: x-large;
    }
    a:link, a:visited
    {
      color: #3333FF;
      text-decoration: none;
      border-bottom: 1px dotted #3333FF;
    }
    a:hover, a:visited:hover
    {
      color: #FF3366;
      text-decoration: none;
      border-bottom: 1px solid #FF3366;
    }
    #pdf
    {
      width: 500px;
      height: 600px;
      margin: 2em auto;
      border: 10px solid #6699FF;
    }
    #pdf p
    {
      padding: 1em;
    }
    #pdf object
    {
      display: block;
      border: solid 1px #666;
    }
  </style>
  <script type="text/javascript" src="pdfobject.js"></script>
  <script type="text/javascript">
    window.onload = function () {
      var success =
        new PDFObject({ url: "http://mer.markit.com/br-reg/PublicReport.action?getDocumentById=true&document_id=103000000000681" }).embed("pdf");
    };
  </script>
</head>
<body>
  <h1>
    Embedding a PDF using PDFObject: Simple example with basic CSS</h1>
  <p>
    This example uses one line of JavaScript wrapped in a <em>window.onload</em> statement,
    with a little CSS added to control the styling of the embedded element.
  </p>
  <div id="pdf">
    It appears you don't have Adobe Reader or PDF support in this web browser. <a href="http://mer.markit.com/br-reg/PublicReport.action?getDocumentById=true&document_id=103000000000681">
      Click here to download the PDF </a>
  </div>
</body>
</html>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15991102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档