在浏览器中打开 PDF
我想在浏览器中打开 PDF,以便编写以下代码,但出现错误:“文件不以 %PDF 开头”。
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div style="width:358px;height:206px;">
<img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_home_image_117713.jpg" border="2" width="358" height="206" />
<img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_mug_image_117713.jpg" border="1" width="68" height="68" style="position:absolute; top:190px; left:25px; z-index:2"/>
<font style="position:absolute; top:15px; left:15px; z-index:2; border:1px; color:FFFFFF;font-family:impact;font-size:14px;">
</font>
</div>
<div style="width:425px;height:206px;padding-top:5px; padding-left:75px; text-align:center; border:1px;font-family:Arial;font-size:9px;">
</div>
</body>
</html>
<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">
<cfcontent type="application/pdf">
I want to open the PDF in browser so that i have wriiten the below code but i have occured the error: "file does not begin with %PDF".
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div style="width:358px;height:206px;">
<img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_home_image_117713.jpg" border="2" width="358" height="206" />
<img src="http://www.pcsamerica.net/mp/images/reloimages/upload/home_mug_image_117713.jpg" border="1" width="68" height="68" style="position:absolute; top:190px; left:25px; z-index:2"/>
<font style="position:absolute; top:15px; left:15px; z-index:2; border:1px; color:FFFFFF;font-family:impact;font-size:14px;">
</font>
</div>
<div style="width:425px;height:206px;padding-top:5px; padding-left:75px; text-align:center; border:1px;font-family:Arial;font-size:9px;">
</div>
</body>
</html>
<cfheader name="Content-Disposition" value="inline; filename=Test.pdf">
<cfcontent type="application/pdf">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只是有一点不行,你不能让 Coldfusion 的 PDF 在浏览器中显示;您只需使用 Colfusion 生成 PDF。 PDF 打开的位置是默认阅读器的选项,最常见的是 Adobe Reader。如果 PDF 未在浏览器中打开,您可能已关闭“在浏览器中显示 PDF”。要修复此问题,请打开 Adobe Reader 并转到“编辑”->“首选项”,在“类别:”下单击“Internet”。在右侧,您将看到“在浏览器中显示 PDF”标签的复选框,确保已单击它。
Just a little side not, you can't make the PDF display in browser from Coldfusion; you’re just generating the PDF with Colfusion. Where the PDF opens up is a option of your default reader, most commonly Adobe Reader. If the PDF isn’t opening up in the browser, you might have “Display PDF in browser” turned off. To Fix that open Adobe Reader and go to Edit->Preference, under "Categories:" click “Internet”. On the right hand side you’ll see a check box, for the “Display PDF in browser” label, make sure it’s clicked.
不知道 Coldfusion,但这看起来确实像是您将 HTML 发送到浏览器并假装它是 PDF。那是行不通的。
Don't know coldfusion, but that really looks like you're sending HTML to the browser and pretending its PDF. That won't work.
认为你应该尝试以下方式:
PS 还没有真正测试过代码,只是一个简单的例子。
Think you should try following way:
P.S. Haven't really tested the code, just a quick example.
从我的一些代码来看:
它必须是唯一的输出。这里的区别是与 cfcontent 一起使用的文件属性
From some of my code:
It must be the ONLY thing output. The difference here is the file attribute being used with cfcontent
我同意 derobert 的答案。您收到的错误消息意味着浏览器通过在收到的文件开头查找字符 %PDF(十六进制:25 50 44 46)来检查该文件是否是有效的 pdf 文件。由于您没有发送 pdf 文件,因此不存在签名,因此会出现错误消息。
I agree with the answer by derobert. The error message you get means that the browser checks if the file is a valid pdf file by looking for the characters %PDF (hex: 25 50 44 46) at the beginning of the file it received. Since you are not sending a pdf file, the signature is not there, hence the error message.
将 HTML 包裹在 cfdocument 标签中,如下所示
这应该可以解决问题
Wrap the HTML in a cfdocument tag like so
That should do the trick