PDF文件无法在浏览器中正确显示

时间:2014-05-01 09:44:52

标签: php pdf

我们正在尝试显示来自Live Server(PHP Verion 5.2)的PDF File,但未正确显示...而不是显示文件bytecode ..

如果我们尝试从本地服务器(PHP版本5.4)显示,则会显示相同的文件。

我们已尝试从服务器(显示ByteCode)显示PDF:

header("Content-type: application/pdf");
header("Content-Length: " . filesize($url_path));  // '$url_path' variable is fullpath to PDF file
readfile($url_path) or die("File not found ");

输出ByteCode:

%PDF-1.5 2 0 obj << >> endobj 3 0 obj << /Type /Page /Parent 1 0 R /MediaBox [0 0 598 834] /Contents 4 0 R /Resources << /XObject << /img0 5 0 R >> /ProcSet [ /PDF /Text /ImageC] /Font 2 0 R >> /Annots 6 0 R >> endobj 4 0 obj << /Filter /FlateDecode /Length 61 >> stream x


如果我们尝试使用下面的线显示,那么它可以正常工作..

echo "<script type='text/javascript'>  window.location.href = '".$url_path."'; </script>";

我们已经尝试更改.htaccess代码,如下所示:

(1) RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+).pdf$  /cgi-bin/pdf.php?file=$1 [L,NC,QSA]


(2) RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^ /viewpdf.php?request_url=%{REQUEST_URI} [L]

仍然没有工作..

3 个答案:

答案 0 :(得分:0)

尝试检查.htaccess文件。

答案 1 :(得分:0)

终于得到了解决方案:

在声明header时,我们应该注意不要在它之前宣布echoprint_r ..

如果我们导入require(..)include(..)require_once(..)中的文件也不应包含任何echo语句..否则之后的所有header声明都将被忽略..

简而言之: 在header语句之上,不应写入echo语句,否则将忽略所有标头声明..

答案 2 :(得分:0)

只需在php中打印类的对象字节码,然后使用标题(&#34; Content-type:application / pdf&#34;)函数打印pdf

相关问题