Failed to load PDF document in chrome browser

Agree with @Sébastien Gicquel, however, for my case, I need to put flush+ob_clean right after header but before @readfile.

FYI,my Chrome version is 9.0.3945.79, and the code works well without ob_clean and flush in Firefox and IE.

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' .$file. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
ob_clean();
flush();
@readfile($file);

This maybe is the problem with generated pdf. If it works on firefox, download the file and try to open it. And if pdf viewer in you pc outputs corrupted pdf, then you might need to tweak your code. I am facing the same problem. Chrome won't open it because of the corrupted pdf.

Hope my answer will let you go to a journey of debugging. Cheers. :D


In my case, the html of the current page was sent in the pdf (i see it when i open the pdf with a simple text editor).

Solution for me flush + ob_clean before sending header

ob_clean();
flush();
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'"); 
echo $result; 
exit;

This also happens when you're using an html to PDF library such as mPDF, and somehow you're sending HTML to the browser before sending the file. Many readers ignore the HTML before reading the PDF markup - Chrome does not.

For instance, in PHP, clear your output buffer before sending the data to mPDF: ob_clean().