如何使用PHP在浏览器中打开PDF文件

如何使用PHP在浏览器中打开PDF文件,php,pdf,Php,Pdf,我是PHP新手。我正在测试是否可以在Apache服务器上的目录中打开PDF文件。该目录没有.htaccess。当我执行代码时,我得到一个“加载PDF文档失败”错误。$filePath很好。任何想法都将不胜感激 $filePath = '/home/xxx/public_html/xFiles/Reports/Test.pdf'; if (file_exists($filePath)) { echo "The file $filePath exists"; } else { ech

我是PHP新手。我正在测试是否可以在Apache服务器上的目录中打开PDF文件。该目录没有.htaccess。当我执行代码时,我得到一个“加载PDF文档失败”错误。$filePath很好。任何想法都将不胜感激

$filePath = '/home/xxx/public_html/xFiles/Reports/Test.pdf';
if (file_exists($filePath)) {
    echo "The file $filePath exists";
} else {
    echo "The file $filePath does not exist";
    die();
}
$filename="Test.pdf";

header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
readfile($filePath);

只是不要在标题和pdf输出之前回显任何内容。
我想这可能破坏了你的pdf数据

$filePath = '/home/xxx/public_html/xFiles/Reports/Test.pdf';
if (!file_exists($filePath)) {
    echo "The file $filePath does not exist";
    die();
}
$filename="Test.pdf";

header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
readfile($filePath);

标题前有输出,这是不允许的。感谢Max的帮助。我有两个问题,一个。正如你所说的。2.我是从网页上的“php代码片段”插件运行这个程序的。如果我将其作为php文件加载到服务器上,它就会工作。再次感谢您的帮助。