如何在php中显示pdf [英] How to display pdf in php
问题描述
我正在尝试使用php显示pdf文件,
<html>
<head>
<style type="text/css">
#myiframe {width:600px; height:100%;}
</style>
</head>
<body>
<div id="scroller">
<iframe name="myiframe" id="myiframe" src="xml.pdf">
</div>
</body>
</html>
它在html中很好地工作,但是当我想将此代码用于php文件时,它什么也不显示,仅尝试下载pdf文件. 有人可以帮我吗?
可以使用很多选项:(均已测试).
有两种方法.
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");
@readfile('path\to\filename.pdf');
或:(请注意转义的双引号).给它分配名称时也需要使用相同的方法.
<?php
echo "<iframe src=\"file.pdf\" width=\"100%\" style=\"height:100%\"></iframe>";
?>
即:name="myiframe" id="myiframe"
需要更改为:
name=\"myiframe\" id=\"myiframe\"
在PHP中.
请务必查看: SO上的此答案 ,以获取有关以下内容的更多选择主题.
脚注:在Windows 8中尝试查看PDF文件时,存在一些已知问题.如果未安装浏览器插件,则安装Adobe Acrobat Reader是查看这些类型的文档的更好方法. /p>
I'm trying to display a pdf file in php, I used:
<html>
<head>
<style type="text/css">
#myiframe {width:600px; height:100%;}
</style>
</head>
<body>
<div id="scroller">
<iframe name="myiframe" id="myiframe" src="xml.pdf">
</div>
</body>
</html>
it works in html well, but when I want to used this code into a php file, it displays nothing and only try to download the pdf file. Can anybody help me?
There are quite a few options that can be used: (both tested).
Here are two ways.
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");
@readfile('path\to\filename.pdf');
or: (note the escaped double-quotes). The same need to be use when assigning a name to it.
<?php
echo "<iframe src=\"file.pdf\" width=\"100%\" style=\"height:100%\"></iframe>";
?>
I.e.: name="myiframe" id="myiframe"
would need to be changed to:
name=\"myiframe\" id=\"myiframe\"
inside PHP.
Be sure to have a look at: this answer on SO for more options on the subject.
Footnote: There are known issues when trying to view PDF files in Windows 8. Installing Adobe Acrobat Reader is a better method to view these types of documents if no browser plug-ins are installed.
这篇关于如何在php中显示pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!