<?php
namespace App\Controller;
use App\Entity\Trainings;
use App\Repository\TrainingsRepository;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Snappy\Pdf;
#[Route('/generate-pdf')]
class PDFController extends AbstractController
{
#[Route('/{id}', name: 'generate_pdf')]
public function generatePDF(Trainings $fiche, Pdf $pdf): PDFResponse
{
$footer = $this->renderView(
'pdf/footer.html.twig');
$options = [
'enable-javascript' => true,
'margin-top' => 10,
'margin-bottom' => 30,
'margin-left' => 10,
'margin-right' => 10,
'page-size' => 'A4',
'enable-local-file-access' => true,
'footer-html' => $footer,
];
$html = $this->renderView(
'pdf/pdf.html.twig', array(
'training' => $fiche,
)
);
if ($fiche->isIsFiche()) {
$trainingTitle = str_replace(' ', '-', $fiche->getFicheTitle());
$trainingTitle = preg_replace('/[^A-Za-z0-9\-]/', '', $trainingTitle);
} else {
$trainingTitle = str_replace(' ', '-', $fiche->getTitle());
$trainingTitle = preg_replace('/[^A-Za-z0-9\-]/', '', $trainingTitle);
}
$pdf->setOptions($options);
return new PdfResponse(
$pdf->getOutputFromHtml($html),
'AFPMA - '.$trainingTitle.'.pdf',
);
}
}