【问题标题】:PDF rendered as textPDF 呈现为文本
【发布时间】:2012-08-27 14:37:43
【问题描述】:

我正在使用 PHP 通过浏览器为我的 Web 应用程序生成 PDF。最近,客户端将网络服务器更改为 Apache,现在此功能不再起作用。浏览器没有生成 PDF,而是将 PDF 显示为文本,就像它忽略 Content-Type(设置为“application/pdf”)一样。其实我通过注释源代码中设置Content-Type这一行成功模拟了这个问题。

我需要关于在哪里寻找什么的想法,非常欢迎任何帮助:)

【问题讨论】:

  • 你如何设置 Content-Type 标头,你能显示你的代码吗?是否在任何输出之前设置了标头?
  • 我愿意:header("Content-type: application/pdf");.是的,标题是在任何输出之前设置的。
  • 那么,您能否转储浏览器实际接收到的标头?也尝试显式设置header("Content-Disposition: inline");

标签: php apache pdf


【解决方案1】:

由于你是通过PHP生成PDF文件,所以可以尝试添加这些头文件:

$file_default = 'default filename you want to appear when the user downloads.pdf';
$file_location = '/path/to/file.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_default);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file_location));
ob_clean();
flush();
readfile($file_location);

【讨论】:

  • 我试过了。它正在开发中,但不在生产中(不同版本的 apache)。我试过“标题('Content-Disposition:附件;文件名='.$file_default);”强制下载pdf,但没有好处。任何想法为什么?可能缺少任何配置?
  • 你得到了什么,浏览器中的文件?
【解决方案2】:

我猜你必须强制 apache 下载 PDF 内容而不是显示:

检查这个:http://www.thingy-ma-jig.co.uk/blog/06-08-2007/force-a-pdf-to-download

【讨论】:

  • 我试过“header('Content-Disposition: attachment; filename='.$file_default);”强迫这种行为。可悲的是,它没有在生产中工作(就像它是内联的一样),但它正在开发中。任何想法为什么?可能缺少任何配置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多