如何用PHP在web浏览器中打开PDF文件?
来源:藏色散人
发布时间:2019-03-19 14:19:42
阅读量:1573
PHP使用标准代码在web浏览器中显示pdf文件。显示pdf文件的过程涉及到pdf文件在服务器上的位置,它使用各种类型的头文件以类型、配置、传输编码等形式定义内容组成。
PHP传递PDF文件以在浏览器上读取它。浏览器要么显示它,要么从localhost服务器下载它,然后显示pdf。
注意:PHP实际上并没有读取PDF文件。它不能识别pdf格式的文件。它只将PDF文件传递给浏览器,以便在浏览器中读取。如果将pdf文件复制到XAMPP的htdocs文件夹中,则不需要指定文件路径。
示例1:
在浏览器上显示pdf文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
$file = 'filename.pdf';
$filename = 'filename.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile($file);
|
输出:

示例2:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$filename = "/path/to/the/file.pdf";
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
readfile($filename);
|