views:

43

answers:

2

Hi, I am creating pdf document for downloading for e.g. someone clicked a PDF link then a pdf is generated and browser opens new window with path of that pdf file. Problem is that browser is giving 404 NOT found error for that file for about 40-50 seconds after its creation but after that when I refresh browser that file is present for viewing or downloading.

one pdf link is http://images.myvouchercodes.co.uk/mvclocal/pdf/ca3b5098-9b35-7d8e.pdf where you can view file but same url gives 404 not found immediately after its creation. I am using following code to write file

      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();
        }

That call is ajax and it echos filename upon pdf completion, then this filename is appended to url and the i use window.open() to open new window with pdf link that gives me 404 not found error. Anybody knows why this error is occuring?

+1  A: 

First, you need to isolate if it's Zend pdf issue or general webserver one. Try to manually creaty dummy 1-byte file and check if it would show the same delay.

They it's up to your webserver config - some might have aggressive file-system properties caching, so there is no universal answer, you'll need to check corresponding config options.

BarsMonster
if I use fread in place of file_get_contents then I got file content is variable but if i take same url to browser then it gives 404 not found. There got to be some other issue like handling delay for writing file on remote server etc. it is definatly not Zend_pdf issue
Ayaz Alavi
+1  A: 

use php headers for directly outputting pdf on browser

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

so instead of doing ajax open new window with url of create pdf code.

markcotech
thats what I did
Ayaz Alavi