Php PDF文件无法在浏览器中正确显示

Php PDF文件无法在浏览器中正确显示,php,pdf,Php,Pdf,我们正在尝试显示一个来自Live Server PHP Verion 5.2的PDF文件,该文件无法正确显示。。。将显示字节码而不是文件 如果我们试图从本地服务器PHP 5.4版显示,则会显示相同的文件 我们已尝试从显示字节码的服务器显示PDF: header("Content-type: application/pdf"); header("Content-Length: " . filesize($url_path)); // '$url_path' variable is fullpat

我们正在尝试显示一个来自Live Server PHP Verion 5.2的PDF文件,该文件无法正确显示。。。将显示字节码而不是文件

如果我们试图从本地服务器PHP 5.4版显示,则会显示相同的文件

我们已尝试从显示字节码的服务器显示PDF:

header("Content-type: application/pdf");
header("Content-Length: " . filesize($url_path));  // '$url_path' variable is fullpath to PDF file
readfile($url_path) or die("File not found ");
输出字节码:

header("Content-type: application/pdf");
header("Content-Length: " . filesize($url_path));  // '$url_path' variable is fullpath to PDF file
readfile($url_path) or die("File not found ");

仍然不工作。

尝试检查.htaccess文件。

最终得到了解决方案:

在声明标头时,我们应注意在其之前不应声明echo或print\r

如果我们在需要时导入文件。。或包括。。或者只需要一次。。也不应包含任何echo语句。。否则,之后的所有标头声明都将被忽略


简而言之:在header语句上方,不应写入任何echo语句,否则将忽略所有header声明

只需在php中打印类的对象字节码,并使用headerContent type:application/pdf函数打印pdf

echo "<script type='text/javascript'>  window.location.href = '".$url_path."'; </script>";
(1) RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+).pdf$  /cgi-bin/pdf.php?file=$1 [L,NC,QSA]


(2) RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^ /viewpdf.php?request_url=%{REQUEST_URI} [L]