【问题标题】:How to open a PDF file in browser using PHP如何使用 PHP 在浏览器中打开 PDF 文件
【发布时间】:2019-02-08 04:23:24
【问题描述】:

我是 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);

【问题讨论】:

  • 你在标题之前有输出,这是不允许的

标签: php pdf


【解决方案1】:

只是不要在标题和 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);

【讨论】:

  • 感谢麦克斯的帮助。我有 2 个问题,1. 正如你所说。 2. 我是从网页上的“php sn-ps”插件运行的。如果我将它作为 php 文件加载到服务器上,它就可以工作。再次感谢您的热心帮助。
猜你喜欢
  • 2017-01-01
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
相关资源
最近更新 更多