json(['message' => 'Still working.....'], 409); } Cache::forget($hash); // Check if the content starts with PDF signature (%PDF-) $isPdf = str_starts_with(trim($report), '%PDF-'); $attachment_name = $isPdf ? 'report.pdf' : 'report.csv'; $headers = [ 'Content-Disposition' => "attachment; filename=\"{$attachment_name}\"", 'Content-Type' => $isPdf ? 'application/pdf' : 'text/csv' ]; // Set appropriate filename extension return response()->streamDownload(function () use ($report) { echo $report; }, $attachment_name, $headers); } }