Generation de pdf en asynchrone en utilisant KnpSnappyBundle

Par youstra, il y a 5 ans


Bonjour
Je veux générer des fichiers pdf en utilisant le composant messenger de Symfony et KnpSnappyBundle. ALors toutes la config est parfaite. J'arrive à envoyer un message au bus.
Mais lors de la capture du message (handler), j'obtiens cette erreur qui dit qu"on ne trouve pas une variable:

08:37:01 INFO [messenger] Received message App\Message\GeneratePDF ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF"] 08:37:01 ERROR [messenger] Error thrown while handling message App\Message\GeneratePDF. Sending for retry #1 using 1000 ms delay. Error: "Impossible to access an attribute ("schemeAndHttpHost") on a null variable." ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF","retryCount" => 1,"delay" => 1000,"error" => "Impossible to access an attribute ("schemeAndHttpHost") on a null variable.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 08:37:02 INFO [messenger] Received message App\Message\GeneratePDF ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF"] 08:37:02 ERROR [messenger] Error thrown while handling message App\Message\GeneratePDF. Sending for retry #2 using 2000 ms delay. Error: "Impossible to access an attribute ("schemeAndHttpHost") on a null variable." ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF","retryCount" => 2,"delay" => 2000,"error" => "Impossible to access an attribute ("schemeAndHttpHost") on a null variable.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 08:37:04 INFO [messenger] Received message App\Message\GeneratePDF ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF"] 08:37:04 ERROR [messenger] Error thrown while handling message App\Message\GeneratePDF. Sending for retry #3 using 4000 ms delay. Error: "Impossible to access an attribute ("schemeAndHttpHost") on a null variable." ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF","retryCount" => 3,"delay" => 4000,"error" => "Impossible to access an attribute ("schemeAndHttpHost") on a null variable.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 08:37:08 INFO [messenger] Received message App\Message\GeneratePDF ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF"] 08:37:08 CRITICAL [messenger] Error thrown while handling message App\Message\GeneratePDF. Removing from transport after 3 retries. Error: "Impossible to access an attribute ("schemeAndHttpHost") on a null variable." ["message" => App\Message\GeneratePDF^ { …},"class" => "App\Message\GeneratePDF","retryCount" => 3,"error" => "Impossible to access an attribute ("schemeAndHttpHost") on a null variable.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]

Je vous donne ma config et mon code.
Config en environnement de dev
Symfony 5.1.11
Messenger 5.1.11
KnpSnappyBundle 1.8.0
MySQL5.7.32
Php 7.4.20

Code
messenger.yaml

framework: messenger: # Uncomment this (and the failed transport below) to send failed messages to this transport for later handling. # failure_transport: failed transports: # https://symfony.com/doc/current/messenger.html#transport-configuration async: '%env(MESSENGER_TRANSPORT_DSN)%' # failed: 'doctrine://default?queue_name=failed' # sync: 'sync://' routing: # Route your messages to the transports 'App\Message\GeneratePDF': async

App\Message\GeneratePDF

<?php namespace App\Message; class GeneratePDF { private $classeId; private $as; private $regime; private $annexeId; public function __construct(int $classeId, int $as, string $regime, int $annexeId){ $this->classeId = $classeId; $this->as = $as; $this->regime = $regime; $this->annexeId = $annexeId; } public function getAnnexeId() { return $this->annexeId; } public function getRegime() { return $this->regime; } public function getClasseId() { return $this->classeId; } public function getAs() { return $this->as; } }

App\MessageHandler\GeneratePDFHandler

<?php namespace App\MessageHandler; use Knp\Snappy\Pdf; use App\Entity\Annee; use App\Entity\Eleve; use App\Entity\Annexe; use App\Entity\Classe; use App\Message\GeneratePDF; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class GeneratePDFHandler extends AbstractController implements MessageHandlerInterface { private $manager; private $snappy; public function __construct(EntityManagerInterface $manager, Pdf $snappy){ $this->manager = $manager; $this->snappy = $snappy; } public function __invoke(GeneratePDF $message) { $repoEleve = $this->manager->getRepository(Eleve ::class); $repoClasse = $this->manager->getRepository(Classe::class); $repoAnnee = $this->manager->getRepository(Annee ::class); $repoAnnexe = $this->manager->getRepository(Annexe::class); $annexe = $repoAnnexe->find($message->getAnnexeId()); $annee = $repoAnnee->find($message->getAs()); $classe = $repoClasse->find($message->getClasseId()); $eleves = $repoEleve->minimumInformationsElevesDUneClasse($message->getClasseId()); $dateNaissanceAr = []; foreach ($eleves as $eleve) { $dateNaissanceAr[$eleve["id"]] = $this->arabic_w2e($eleve["dateNaissance"]); } $libelleAnneeAr = $this->arabic_w2e($annee->getLibelle()); $filename = "liste-de-classe-de-".$classe->getLibelleFr(); $options = [ 'orientation' => 'portrait', 'enable-javascript' => false, 'javascript-delay' => 1000, 'no-stop-slow-scripts' => true, 'background' => true, 'lowquality' => false, 'page-width' => 86, 'page-height' => 54, 'margin-bottom' => 0, 'margin-left' => 0, 'margin-right' => 0, 'margin-top' => 0, 'encoding' => 'utf-8', 'images' => true, 'cookie' => array(), 'dpi' => 300, 'enable-external-links' => false, 'enable-internal-links' => false, ]; $expiration = "Mai 2022"; $html = $this->renderView('direction-scolarite/scolarite/cartes-scolaires.html.twig', [ "annee" => $annee, "classe" => $classe, "eleves" => $eleves, 'annexe' => $annexe, 'expiration' => $expiration, 'dateNaissanceAr' => $dateNaissanceAr, 'libelleAnneeAr' => $libelleAnneeAr, 'server' => $_SERVER["DOCUMENT_ROOT"], ]); // $this->snappy->getOutputFromHtml($html, $options); return new Response( $this->snappy->getOutputFromHtml($html, $options), 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename="'.$filename.'.pdf"' ] ); } public function arabic_w2e($str) { $arabic_eastern = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'); $arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); return str_replace($arabic_western, $arabic_eastern, $str); } }

Logique métier

/** * @Security("is_granted('ROLE_SCOLARITE') or is_granted('ROLE_SCOLARITE_ANNEXE')") * @Route("/cartes-scolaires-des-eleves-d-une-classe-{as}-{regime}-{classeId}-{annexeId}", name="isi_cartes_scolaires_d_une_classe") */ public function cartesScolaireDesEleves(EntityManagerInterface $manager, int $as, string $regime, int $classeId, int $annexeId) { $repoAnnexe = $manager->getRepository(Annexe::class); $annexe = $repoAnnexe->find($annexeId); if(!in_array($annexeId, $this->getUser()->idsAnnexes()) or (in_array($annexeId, $this->getUser()->idsAnnexes()) and $this->getUser()->findAnnexe($annexeId)->getDisabled() == 1)){ $this->addFlash('danger', 'Vous n\'êtes pas autorisés à exploiter les données de l\'annexe <strong>'.$annexe->getLibelle().'</strong>.'); return $this->redirect($this->generateUrl('annexes_homepage', ['as' => $as])); } $this->dispatchMessage(new GeneratePDF($classeId, $as, $regime, $annexeId)); return $this->redirectToRoute('isi_gestion_classes', ["as" => $as, "regime" => $regime, "annexeId" => $annexeId]); }

.env

MESSENGER_TRANSPORT_DSN=doctrine://default

Pourriez-vous me dire ce que je dois faire pour résoudre le problème ?

Désolé pour ce long sujet, mais je tenais à mettre à disposition tout le nécessaire.

Aucune réponse