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.
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();
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”.
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.
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.
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;
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With