PDF呈现为文本


PDF rendered as text

我使用PHP通过浏览器为我的web应用程序生成PDF。最近,客户端将Web服务器更改为Apache,现在此功能已不起作用。浏览器没有生成PDF,而是将PDF显示为文本,就像它忽略内容类型(设置为"application/PDF")一样。事实上,我通过注释源代码中设置ContentType的行,成功地模拟了这个问题。

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

由于您通过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下载PDF内容,而不是显示:

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