src/Controller/PDFController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Trainings;
  4. use App\Repository\TrainingsRepository;
  5. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Knp\Snappy\Pdf;
  11. #[Route('/generate-pdf')]
  12. class PDFController extends AbstractController
  13. {
  14.     #[Route('/{id}'name'generate_pdf')]
  15.     public function generatePDF(Trainings $fichePdf $pdf): PDFResponse
  16.     {
  17.         $footer $this->renderView(
  18.             'pdf/footer.html.twig');
  19.         $options = [
  20.             'enable-javascript' => true,
  21.             'margin-top' => 10,
  22.             'margin-bottom' => 30,
  23.             'margin-left' => 10,
  24.             'margin-right' => 10,
  25.             'page-size' => 'A4',
  26.             'enable-local-file-access' => true,
  27.             'footer-html' => $footer,
  28.         ];
  29.         $html $this->renderView(
  30.             'pdf/pdf.html.twig', array(
  31.                 'training' => $fiche,
  32.             )
  33.         );
  34.         if ($fiche->isIsFiche()) {
  35.             $trainingTitle str_replace(' ''-'$fiche->getFicheTitle());
  36.             $trainingTitle preg_replace('/[^A-Za-z0-9\-]/'''$trainingTitle);
  37.         } else {
  38.             $trainingTitle str_replace(' ''-'$fiche->getTitle());
  39.             $trainingTitle preg_replace('/[^A-Za-z0-9\-]/'''$trainingTitle);
  40.         }
  41.         $pdf->setOptions($options);
  42.         return new PdfResponse(
  43.             $pdf->getOutputFromHtml($html),
  44.             'AFPMA - '.$trainingTitle.'.pdf',
  45.         );
  46.     }
  47. }