0

我正在创建 pdf 文档以供下载,例如有人单击 PDF 链接,然后生成 pdf,浏览器会打开带有该 pdf 文件路径的新窗口。问题是浏览器在创建后大约 40-50 秒内为该文件提供 404 NOT found 错误,但在那之后,当我刷新浏览器时,该文件存在以供查看或下载。

一个 pdf 链接是http://images.myvouchercodes.co.uk/mvclocal/pdf/ca3b5098-9b35-7d8e.pdf ,您可以在其中查看文件,但相同的 url 在创建后立即找不到 404。我正在使用以下代码编写文件

      try{
            $fh = fopen($filename, "w");                        
            $contents = $this->render();   // return pdf contents in string        
            if(fwrite($fh, $contents))
            {           
                $fh = fopen($filename, "r");                    
                while(strlen(file_get_contents($filename)) !=  strlen($contents))
                { }
                echo $filename;
            }
            else
            {
                throw new Exception ("Unable to create pdf");
            }
            fclose($fh);

        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }

该调用是 ajax,它在 pdf 完成时回显文件名,然后将此文件名附加到 url,我使用 window.open() 打开带有 pdf 链接的新窗口,这给了我 404 not found 错误。有谁知道为什么会发生这个错误?

4

2 回答 2

1

使用 php headers 在浏览器上直接输出 pdf

$contents = $this->render();
header('Content-type: application/pdf');
header('Content-disposition: inline; filename=nijman.pdf');
header('Content-length: ' . strlen($contents));
echo $contents;

因此,不要使用创建 pdf 代码的 url 打开 ajax 新窗口。

于 2010-08-25T10:08:43.073 回答
1

首先,您需要隔离它是 Zend pdf 问题还是一般网络服务器问题。尝试手动创建虚拟 1 字节文件并检查它是否会显示相同的延迟。

它们取决于您的网络服务器配置 - 有些可能具有激进的文件系统属性缓存,因此没有通用答案,您需要检查相应的配置选项。

于 2010-08-24T10:30:45.753 回答