当“在浏览器中显示PDF”选项未选中时,是否有一种方法强制PDF文件在浏览器中打开?
我尝试使用嵌入标签和一个iframe,但它只有当该选项被选中时才会工作。
我该怎么办?
当“在浏览器中显示PDF”选项未选中时,是否有一种方法强制PDF文件在浏览器中打开?
我尝试使用嵌入标签和一个iframe,但它只有当该选项被选中时才会工作。
我该怎么办?
当前回答
虽然下面的工作很好在firefox,它不工作在chrome和移动浏览器。
Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
要修复chrome和移动浏览器错误,请执行以下操作:
将文件存储在项目中的某个目录中 使用谷歌PDF查看器
谷歌PDF查看器可以这样使用:
<iframe src="http://docs.google.com/gview?url=http://example.com/path/to/my/directory/pdffile.pdf&embedded=true" frameborder="0"></iframe>
其他回答
要么使用
<embed src="file.pdf" />
如果嵌入是一个选项或我的新插件,PIFF: https://github.com/terrasoftlabs/piff
虽然下面的工作很好在firefox,它不工作在chrome和移动浏览器。
Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
要修复chrome和移动浏览器错误,请执行以下操作:
将文件存储在项目中的某个目录中 使用谷歌PDF查看器
谷歌PDF查看器可以这样使用:
<iframe src="http://docs.google.com/gview?url=http://example.com/path/to/my/directory/pdffile.pdf&embedded=true" frameborder="0"></iframe>
如果你有Apache,把这个添加到。htaccess文件中:
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
下面是在PHP中强制在浏览器中查看文件的另一种方法:
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
$url = 'uploads/'.$file_name;
echo '<html>'
.header('Content-Type: application/'.$extension).'<br>'
.header('Content-Disposition: inline; filename="'.$file_name.'"').'<br>'
.'<body>'
.'<object style="overflow: hidden; height: 100%;
width: 100%; position: absolute;" height="100%" width="100%" data="'.$url.'" type="application/'.$extension.'">
<embed src="'.$url.'" type="application/'.$extension.'" />
</object>'
.'</body>'
. '</html>';
如果你链接到一个。pdf文件,它会在浏览器中打开。 如果该框未选中,它应该链接到一个。zip以强制下载。
如果.zip文件不是一个选项,那么使用PHP中的头文件强制下载
header('Content-Type: application/force-download');
header('Content-Description: File Transfer');