【问题标题】:PHP not displaying PDF file in Google ChromePHP 未在 Google Chrome 中显示 PDF 文件
【发布时间】:2014-08-28 08:01:35
【问题描述】:

我想使用此代码在浏览器中显示 PDF 文件

<?php
    $file = './path/to/the.pdf';
    $filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */

    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $filename . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($file));
    header('Accept-Ranges: bytes');

    @readfile($file);
?>

该代码在 Firefox 中运行良好,但在 Chrome 中却不行。我还没有在任何其他浏览器中测试过。这是 Chrome 的错误吗?无论如何我可以让它显示在 Firefox 和 Chrome 中吗?

【问题讨论】:

  • 好吧,chrome 会强制你的 pdf 下载吗?还有你为什么在readfile之前使用@?
  • 可能是标题问题。
  • 这段代码对我有用。您是否尝试过更新您的 chrome,或者在其他一些 comp 上尝试它?也许有一些插件可能会导致问题?
  • Chrome 不会强制下载 pdf,只是停留在加载图标上。用firefox没问题。
  • 我遇到了完全相同的问题。我注意到它只发生在大于 64K 的 PDF 文件中。 chrome 控制台(网络)报告 Status Code:206 Partial Content...

标签: php google-chrome pdf


【解决方案1】:

这取决于您的浏览器设置,而不是代码。通常默认情况下,firefox 会在浏览器中打开 pdf,而在 chrome 中则不会

【讨论】:

  • 我在 Firefox 中使用相同的代码打开 pdf 没有问题,只需使用 chrome。它只是在左下角显示加载图标,但从不打开 pdf。
  • 不,这不是浏览器设置...该代码适用于小型 PDF (
【解决方案2】:

只需将内容配置更改为附件

 header('Content-Disposition: attachment;; filename="' . $filename . '"');

这不会在浏览器中显示 PDF,但在 Chrome 中似乎可以工作。

【讨论】:

    【解决方案3】:

    我之前也遇到过同样的问题,但并不是所有的电脑都有这个问题。就我而言,我使用 Adob​​e 或默认的 Chrome PDF 阅读器进行了一些设置。检查您的设置。

    【讨论】:

    • 我应该检查哪个设置?
    【解决方案4】:

    $file 只是没有文件名本身的文件路径吗?您是否应该使用这种情况:

    header('Content-Length: ' . filesize("$file/$filename"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2017-07-07
      相关资源
      最近更新 更多