14

I'm using mPDF library for generating PDF files from HTML page. It is working nice in firefox but it is not display PDF file in chrome browser.

I'm getting following error while generate PDF in chrome.

Getting error in chrome browser while generating PDF

Following is my code for generate PDF using mPDF

ob_clean();
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $yourFileName . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
$mpdf = new PDF( 'c','A4','','',15, 15,10,14,0,0);
$mpdf->useOnlyCoreFonts = false;
$mpdf->SetDisplayMode('real');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$stylesheet = file_get_contents(APPPATH . 'third_party/mpdf/style.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
Ketav
  • 760
  • 1
  • 8
  • 27

11 Answers11

9

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;
Sébastien Gicquel
  • 4,227
  • 7
  • 54
  • 84
8

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().

jorisw
  • 875
  • 1
  • 11
  • 17
  • Thanks! I have just added ob_clean() before $mpdf->Output(); and it's now working perfect for me in chrome. ob_clean(); $mpdf->Output($name,"D"); – Hina Halani Jul 01 '20 at 10:53
7

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

Hamad Rakshani
  • 115
  • 2
  • 11
  • Same problem here. When I email the pdf generated as attachment, it is downloadable from email, but is corrupted. But, if I view the file in any browser, it opens correctly. Any ideas? – Kumar Kush May 22 '17 at 21:30
  • I am using MPDF library. The problem is now resolved. Thanks anyways. – Kumar Kush Jan 03 '18 at 17:05
  • Can you also let me know how did you solved the problem? Because, I myself am facing the issue on chrome. Was able to do it on Firefox but no luck in chrome. – Hamad Rakshani Jan 03 '18 at 20:57
  • 1
    I simply changed the PDF viewer plugin in Chrome – Kumar Kush Jan 11 '18 at 17:33
2

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);
Shiyu Fang
  • 21
  • 1
1

You may find that Chrome has a problem with the header of the name: Content-Type value: charset=utf-8. Removing the respond header value fixed it for me. (original post)

MeSo2
  • 450
  • 1
  • 7
  • 18
0

This is an issue people were running into on a much older version of Chrome. If you are still seeing this issue, do the following

In Google Chrome, you have 2 options to view PDF files. You can either use Chrome pdf viewer (default) or you can use Adobe Reader

Can you check chrome://plugins (type it in address bar) ? and switch to other PDF viewer (Chrome/Adobe) by just enabling it !

Geeks Parthiban
  • 114
  • 2
  • 10
  • 17
    How is this a solution? should we ask our customers to do this? – lloiacono Sep 07 '16 at 13:56
  • 2
    @lloiacono someone has to. If you don't ask them, someone else would have to. The issue is with Chrome, not with your end. They would see this message for all PDF documents opening on Chrome – Slav Mar 02 '17 at 04:16
  • I get this error with the present-latest version of Chrome (60.0.3112.101) on both Mac and Windows. Works fine with Safari/Mac and whatever PDF reader IE11/Windows uses. Other PDF documents open fine on Chrome. I'm going to debug my PDF generator and the contents passed to it. – jorisw Aug 23 '17 at 09:41
  • the same problem with me, generate pdf from html using mpdf. no problem with other browser except the chrome. I set to force download PDF document (Download PDF files instead of automatically opening them in Chrome) or I install pdf viewer extention for chrome :) – bungdito Feb 12 '18 at 18:55
  • https://stackoverflow.com/questions/45101160/how-to-make-mpdf-6-1-work-with-php-7-1-5 – Montaser El-sawy May 06 '20 at 00:30
0

I was also facing the same "Failed to load PDF Document" issue but applying ob_clean(); before sending header resolved the error as this function discards the contents of the output buffer

cdsln
  • 830
  • 1
  • 11
  • 33
harsh
  • 1
  • 1
0

In my case, I found out that I had some php warnings before the PDF was created, and these warnings caused pdf to go corrupt, and hence became not readable by the Browser. If you want to get rid of this problem, try to keep in check warnings using the following code:

error_reporting(E_ERROR | E_PARSE);

I came to learn about this by the answer @Mr.Web posted originally in LINK

H. M..
  • 568
  • 6
  • 15
-1

The following code block did the work for me in C# for the case of Chrome in browser pdf opening with MemoryStream:

MemoryStream ms;
ms = new MemoryStream(result.ResponseData[0].Report);
HttpContext context = HttpContext.Current;
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "inline;filename=" + Guid.NewGuid().ToString() + "." + _exportType);
context.Response.AddHeader("Content-Length", ms.Length.ToString());            
context.Response.BinaryWrite(ms.ToArray());            
context.Response.Flush();
context.Response.Close();
context.Response.End();
aMJay
  • 2,215
  • 6
  • 22
  • 34
Askeroglu
  • 1
  • 1
-1

In Asp.net Core 2.10, this is worked fine for me.

MemoryStream ms1;
ms1 = new MemoryStream(res);
HttpContext context = _httpContextAccessor.HttpContext;
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.Headers.Add(
    "Content-Disposition",
    "inline;filename=" + Guid.NewGuid().ToString() + "." + "pdf"
);
context.Response.Headers.Add("Content-Length", ms1.Length.ToString());
context.Response.Body.Write(ms1.ToArray());
context.Response.Body.Flush();
context.Response.Body.Close();

Hope this is helpfull.

linktoahref
  • 7,812
  • 3
  • 29
  • 51
Raju Paladiya
  • 778
  • 2
  • 12
  • 35
-1

I had the same issue, tried everything but nothing worked, the bit that did it for me was adding the following straight after $mpdf = new \Mpdf\Mpdf and before generating the PDF;

ob_end_clean();
helvete
  • 2,455
  • 13
  • 33
  • 37
Ali Kassar
  • 29
  • 2