0

I have a pdf generated invoice witch i like to display inline in the browser: link is for example: guest/view/invoice/vqhH8sujcOrpg0t2nF4WQExDLKXmYTNe No extension.

the below code gets a failed to load PDF document when i change it to attachment it works but give a download while i like to display it inline.

$file = site_url('guest/view/generate_invoice_pdf/' . $invoice_url_key);
            $filename = 'factuur.pdf';
            header("Content-type: application/pdf");
            header('Content-Disposition: inline; filename="' . $filename . '"');
            header('Content-Transfer-Encoding: binary');
            header('Accept-Ranges: bytes');
            echo $file;
  • See https://stackoverflow.com/questions/22976041/chrome-not-rendering-pdf-file-without-extension – Laurent DECLERCQ a.k.a Nuxwin Oct 18 '19 at 23:12
  • 5
    Possible duplicate of [Chrome has "Failed to load PDF document" error message on inline PDFs](https://stackoverflow.com/questions/5670785/chrome-has-failed-to-load-pdf-document-error-message-on-inline-pdfs) – gre_gor Oct 18 '19 at 23:17

1 Answers1

0

Sorry i though i did a thorough search but i din't solution is this: Orginal from: Chrome has "Failed to load PDF document" error message on inline PDFs

I've been wrestling with this same issue. This is as close as I got to consistent results across browsers. I think that the reason you could be having problems is if some PDF's are too large for readfile() to handle correctly. Try this:

$fp = fopen($file, "r") ;

header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$myFileName."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file));
ob_clean();
flush();
while (!feof($fp)) {
   $buff = fread($fp, 1024);
   print $buff;
}
exit;````