Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load PDF document in chrome browser

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();
like image 580
Ketav Avatar asked Jan 12 '16 07:01

Ketav


People also ask

Why are PDF files not opening in Google Chrome?

Open Chrome Settings. Click on ''Site settings''. Click on the ”Advanced” button at the bottom. Click on the toggle switch of the heading ”Download PDF files instead of automatically opening them”.

Why does my PDF say failed to load?

The “Failed to Load PDF Document” error message indicates that the web browser you are using, Google Chrome, is trying to open the electronic transcript within its own native PDF viewer. Because the transcript is a secured PDF, it must be opened with Adobe Acrobat Reader.

Why PDF is not opening in browser?

Try resetting the display preference in your browser to clear up the viewing issue. In Reader or Acrobat, right-click the document window, and choose Page Display Preferences. From the list at left, select Internet. Deselect Display PDF in browser, and then click OK.


2 Answers

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;
like image 165
Sébastien Gicquel Avatar answered Sep 30 '22 02:09

Sébastien Gicquel


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);
like image 26
Shiyu Fang Avatar answered Sep 30 '22 04:09

Shiyu Fang