无法使用 PHP 在浏览器上阅读/下载 PDF

问题描述 投票:0回答:0

我有一个 PDF 文件存储在本地主机的一个文件夹中,但在尝试查看文档时出现“Failed to load PDF document.”。

我的代码如下。我检查了完整的文件路径,如果我只是将文件路径复制并粘贴到我的浏览器上,我可以手动读取文件。

`if (file_exists($pdf_path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="' . basename($pdf_path) . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($pdf_path));
    ob_clean();
    flush();
    readfile($pdf_path);
    exit;
} else {
    // Send a 404 Not Found error and exit
    header('HTTP/1.0 404 Not Found');
    die('The requested file could not be found.');
}`

我尝试了其他答案,例如 thisCan't download PDF (FPDF) from PHP function 但都没有用。

php wordpress pdf mpdf
© www.soinside.com 2019 - 2024. All rights reserved.