将PDF文件从在线资源嵌入网页,这需要下载HTTP标头中带有"Content-Disposition:"标签的文件



我需要在网页上显示来自第三方的PDF文件。我有链接到文件,因为他们出现在源页面上。不幸的是,这些链接都不是到文档的实际链接,而是带有某些参数的GET请求,或者其他间接引用,例如:

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

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

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

这个"立"one_answers"倒"在大多数浏览器中都很优雅。同时使用<object><embed>对我有效,并且,据我测试,不影响我下面描述的问题(如果我错了请告诉我)。

当网站在http头中要求下载上面提到的标签时,问题就开始了。例如,下面的文档链接:

http://mer.markit.com/br-reg/PublicReport.action?getDocumentById=true& document_id = 103000000000681

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

我已经绞尽脑汁3天了,想不明白是怎么回事。

也许有一种方法可以以某种方式捕获请求的标头并忽略它们,或者可能强制"可见性"进入GET请求。

一般来说,这是Ruby on Rails应用程序的一部分,所以解决方案应该来自这些行。我在这里没有给出任何ROR代码,因为它似乎不是一个值得关注的来源。

任何直接的解决方案都会被祈祷,而任何其他的-非常感谢。

我想到的替代解决方案和丢弃注释:

  1. 提前下载所有这些文件到本地存储,然后从那里提供它们。必要的存储容量约为1TB,并且还在不断增长,因此将其存储在服务器上将是昂贵的对于小型商业SaaS来说。

  2. 在可能需要的时候缓存这些文档。例如,当某人打开项目的页面时,后台的进程下载相关的pdf文件,因此如果用户单击文档链接,他将获得刚刚下载到本地存储的文档。缓存可以保存几个小时或几天,以防用户返回。这可能是可行的,但如果用户基础很大,那么这个解决方案就会出现与上面的解决方案相同的问题。也是在这个时候,我不知道如何去实现这种算法(非常初学者,你看)

您可能想要查看使用http://pdfobject.com或可能只是适应它的一些代码,因为它似乎能够做您想要的。我做了一个概念验证:

<!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>

最新更新