PHP传递PDF文件以在浏览器上读取它。浏览器要么显示它,要么从localhost服务器下载它,然后显示pdf。
注意:PHP实际上并没有读取PDF文件。它不能识别pdf格式的文件。它只将PDF文件传递给浏览器,以便在浏览器中读取。如果将pdf文件复制到XAMPP的htdocs文件夹中,则不需要指定文件路径。
示例1:
在浏览器上显示pdf文件。
<?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:
<?php // PDF文件在服务器上的位置 $filename = "/path/to/the/file.pdf"; // Header content type header("Content-type: application/pdf"); header("Content-Length: " . filesize($filename)); // 将文件发送到浏览器。 readfile($filename);
输出: