Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP FPDF Failed to load PDF document in Chrome

I am trying to generate a pdf document using fpdf library with php but I am getting an error in chrome as Failed to load PDF document. I am able to view the document properly in firefox. Below is the code I am trying to generate pdf. Can anyone please help? Thanks in advance.

<?php
 ini_set('display_startup_errors', 1);
 ini_set('display_errors', 1);
 error_reporting(-1);
 ob_start();
 $pdf = new FPDF();
 $pdf->AddPage();
 $pdf->SetFont('Arial', 'B', 16);
 $pdf->Cell(40, 10, 'Hello World!');
 header('Content-type: application/pdf');
 header('Content-Transfer-Encoding: binary');
 header("Content-Disposition: inline; filename='example2.pdf'");
 $pdf->Output('example2.pdf', 'I');
 ob_end_flush();
?>
like image 622
krishna89 Avatar asked Nov 20 '22 03:11

krishna89


1 Answers

I know this old question, but there are requests for answers in the comments. Do not set the header. FPDF generates them.

<?php
   require('fpdf.php');

   $pdf = new FPDF();
   $pdf->AddPage();
   $pdf->SetFont('Arial','B',16);
   $pdf->Cell(40,10,'Hello World!');
   $pdf->Output('example2.pdf', 'I');
?>

I also recommend http://www.fpdf.org/

like image 99
websky Avatar answered Dec 19 '22 02:12

websky