requestStack->getCurrentRequest(); if (!$request) { return new Response('Missing request.', Response::HTTP_BAD_REQUEST); } $yearRaw = (string) $request->query->get('year'); if (!preg_match('/^\d{4}$/', $yearRaw)) { throw new UnprocessableEntityHttpException('year must use YYYY format.'); } $year = (int) $yearRaw; $monthRaw = (string) $request->query->get('month', ''); $month = null; if ('' !== $monthRaw) { if (!preg_match('/^(?:0?[1-9]|1[0-2])$/', $monthRaw)) { throw new UnprocessableEntityHttpException('month must be between 1 and 12.'); } $month = (int) $monthRaw; } if (null !== $month) { $from = new DateTimeImmutable(sprintf('%d-%02d-01', $year, $month)); $to = $from->modify('last day of this month'); } else { $from = new DateTimeImmutable("{$year}-01-01"); $to = new DateTimeImmutable("{$year}-12-31"); } $employees = $this->employeeRepository->findAll(); usort($employees, fn ($a, $b) => ($a->getLastName() ?? '') <=> ($b->getLastName() ?? '')); $entries = $this->exportBuilder->buildForEmployees($employees, $from, $to); $options = new Options(); $options->set('isRemoteEnabled', true); $dompdf = new Dompdf($options); $html = $this->twig->render('employee-yearly-hours/print-all.html.twig', [ 'entries' => $entries, 'year' => $year, 'month' => $month, ]); $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); $filename = null !== $month ? sprintf('heures_tous_%d-%02d.pdf', $year, $month) : sprintf('heures_tous_%d.pdf', $year); return new Response($dompdf->output(), Response::HTTP_OK, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename="'.$filename.'"', ]); } }