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?
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.