I have this for the moment :
My controller:
#[Route('/statistic')]
class StatisticController extends AbstractController
{
#[Route('/team-time/{teamId}/{startString}/{endString}', defaults: ['teamId' => null, 'startString' => null, 'endString' => null], name: 'statistic_team_time', methods: ['GET', 'POST'])]
public function teamTime(
Request $request,
EntityManagerInterface $em,
StatisticService $statisticService,
TimeService $timeService,
SessionInterface $session,
StatisticXlsxGenerate $statisticXlsxGenerate,
?string $teamId = null,
?string $startString = null,
?string $endString = null
): Response {
$teamId ??= $request->get("workforce");
$startString ??= $request->get("start");
$endString ??= $request->get("end");
[$start, $end] = $this->getStartAndEndDates($startString, $endString);
$team = $teamId ? $em->getRepository(Team::class)->find($teamId) : null;
if (!$this->isTurboRequest($request)) {
$form = $this->createForm(TeamTimeType::class, [
'workforce' => $team,
'start' => $start,
'end' => $end,
]);
$form->handleRequest($request);
if ($form->get('export')->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$teamId = $data['workforce'];
$start = $data['start'];
$end = $data['end'];
return $this->downloadXlsx($teamId, $start, $end, $statisticService, $em, $statisticXlsxGenerate);
}
return $this->render('statistic/workforce_time.html.twig', [
'form' => $form->createView(),
'title' => 'team_time_statistic',
'route' => 'statistic_team_time',
'searchOnLoad' => (null !== $teamId),
]);
}
$stats = $statisticService->getTeamTime($team, $start, $end);
$chart = $statisticService->getWorkforceTimeChart($stats);
$session->set('team_stats', $stats);
return $this->renderTurbo('statistic/_workforce_time.html.twig', [
'stats' => $stats,
'workedDays' => $timeService->getWorkDayDurationBetweenDates($start, $end),
'chart' => $chart,
'teamSize' => count($team->getWorkers()),
]);
}
private function downloadXlsx(
$teamId,
$start,
$end,
StatisticService $statisticService,
EntityManagerInterface $em,
$statisticXlsxGenerate
): StreamedResponse {
if (!$teamId || !$start || !$end) {
throw new InvalidArgumentException("Missing parameters.");
}
$team = $em->getRepository(Team::class)->find($teamId);
if (!$team) {
throw $this->createNotFoundException("Team not found.");
}
$stats = $statisticService->getTeamTime($team, $start, $end);
$response = $statisticXlsxGenerate->generateXlsx($stats);
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment; filename="team_time_stats.xlsx"');
$response->headers->set('Cache-Control', 'max-age=0');
$response->headers->set('Expires', '0');
$response->headers->set('Pragma', 'public');
$response->headers->set('X-Accel-Buffering', 'no');
return $response;
}
And this is the function generating the XLSX File in StatisticXlsxGenerate
Service :
public function generateXlsx(array $stats): StreamedResponse
{
$response = new StreamedResponse(function () use ($stats) {
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$this->setHeader($sheet);
$this->populateData($sheet, $stats);
$this->finalizeSheet($sheet);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
});
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment; filename="team_time_stats.xlsx"');
$response->headers->set('Cache-Control', 'max-age=0');
$response->headers->set('Expires', '0');
$response->headers->set('Pragma', 'public');
$response->headers->set('X-Accel-Buffering', 'no');
return $response;
}
I tried almost every header possible, Tried changing them on both the Controller and the XlsxGenerator Service but I always have the preview of the file but no file downloaded.
Anyone has this issue before or knows how to solve this kind of issues ?
Thank you in advance.