I\'m getting this output when using FPDF library to generate a pdf file.
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�U��n�0��<�ˁ�7��8\'�!Z��
You need to add an exit(); at the end of your code. taken from https://expressionengine.com/forums/archive/topic/188158/pdf-and-http-headers and tested its working with php 7.2 with codeigniter 4.0
The following headers and Output command is what I currently use with FPDF:
// Set a filename
$filename = "AppName_Day_".$day1."_gen_".date("Y-m-d_H-i").".pdf";
// Send headers
header("Content-Type: application/pdf");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary ");
// Blast out the PDF
$pdf->Output('php://output');
It's worth noting, my use case is a dynamic document that could change at the next download, so I never want a browser to cache it. It's also ALWAYS a download and never viewed in the browser, so the content-disposition: attachment
may not apply to your use case.