我需要在网页上显示来自第三方的PDF文件。我有指向源页面上显示的文档的链接。不幸的是,这些链接都不是指向文档的实际链接,而是带有特定参数的 GET 请求,或者像这样的其他间接引用:
http://cdm.unfccc.int/UserManagement/FileStorage/SNM7EQ2RUD4IA0JLO3HCZ8BTK1VX5P
如果网站不使用 Content-Disposition: attachment;
强制下载响应 header 中的标记,如上所示,那么我可以通过以下方式轻松实现必要的显示:
<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 header 中使用上述标记进行下载时,问题就开始了。例如,以下链接上的文档:
http://mer.markit.com/br-reg/PublicReport.action?getDocumentById=true&document_id=103000000000681
不会通过我上面显示的 HTML 结构显示。它优雅地下降,下载链接工作正常,但我需要查看它!
我已经用头撞墙 3 天了,想不通。
也许有一种方法可以以某种方式捕获请求的 header 并忽略它们,或者可以将“可见性”强加到 GET 请求中。
对于一般信息,这是 Ruby on Rails 应用程序的一部分,因此解决方案应该来自这些方面。我不会在这里提供任何 ROR 代码,因为它似乎不是问题的根源。
我们会祈祷任何直接的解决方案,而任何其他解决方案 - 非常感谢。
我想到的替代解决方案并丢弃评论:
提前将所有这些文件下载到本地存储,然后从那里提供它们。
<
必要的存储容量大约为 1TB,并且还在不断增长,因此将其存储在服务器上对于小型商业 SaaS 来说是昂贵的。在可能需要时缓存这些文档。例如,当有人打开项目页面时,后台进程会下载相关的 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>
关于html - 将 PDF 文件从在线资源嵌入网页,这需要下载 HTTP header 中带有 "Content-Disposition:"标记的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15991102/