$filePath = '路径.pdf';
$this->preview_pdf($filePath);
public function preview_pdf($filePath) {
// PDF文件是否存在
if (!file_exists($filePath)) {
throw new Exception('文件不存在');
}
// 设置header来指示浏览器打开PDF文件进行预览
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . basename($filePath) . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
// 读取并发送PDF文件内容
readfile($filePath);
exit;
}
如何预览 pdf文件
于 2024-04-11 16:14:10 首次发布
本文介绍了一个PHP函数,它通过设置HTTP头来实现在Web浏览器中直接预览PDF文件,当PDF文件存在时,使用`readfile()`函数发送文件内容给用户。
1412

被折叠的 条评论
为什么被折叠?



