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();
?>
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/
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