src/Controller/PdfGeneratorController.php line 65

Open in your IDE?
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Dompdf\Dompdf;
  9. use Picqer\Barcode\BarcodeGeneratorHTML;
  10. use Sylius\Component\Mailer\Sender\SenderInterface;
  11. class PdfGeneratorController extends AbstractController
  12. {
  13.     public function carteCadeau(SenderInterface $sender): Response
  14.     {
  15.         $generator = new BarcodeGeneratorHTML();
  16.         $barcode $generator->getBarcode('0000100000'$generator::TYPE_CODE_128);
  17.         
  18.         
  19.         $data = [
  20.             // 'logo'  => $this->imageToBase64($this->getParameter('kernel.project_dir') . '/public/webapic-theme/images/logo-pdf.png'),
  21.             // 'coci'  => $this->imageToBase64($this->getParameter('kernel.project_dir') . '/public/webapic-theme/images/coccinelle.png'),
  22.             'fond'          => $this->imageToBase64($this->getParameter('kernel.project_dir') . '/public/webapic-theme/images/pdfcadeau-fond-barcode-date.jpg'),            
  23.             'name_dest'     => 'Jojo',
  24.             'name_from'     => 'Juju',
  25.             'montant'       => '4000',
  26.             'num'           => '0000100000',
  27.             'code'          => '123321',
  28.             'message'       => '!!!!',
  29.             'barcode'       => $barcode,
  30.             'date'            => '01/01/2021'    
  31.         ];
  32.         $html =  $this->renderView('pdf/carte-cadeau-date.html.twig'$data);
  33.         $dompdf = new Dompdf();
  34.         $dompdf->loadHtml($html);
  35.         $dompdf->render();
  36.         
  37.         $output $dompdf->output();
  38.         file_put_contents($this->getParameter('kernel.project_dir') . '/public/media/pdf/cartecadeau.pdf'$output);
  39.        
  40.         // $pdf =  new Response (
  41.         //     $dompdf->stream('resume', ["Attachment" => true]),
  42.         //     Response::HTTP_OK,
  43.         //     ['Content-Type' => 'application/pdf']
  44.         // );
  45.         $filepath $this->getParameter('kernel.project_dir') . '/public/media/pdf/cartecadeau.pdf';
  46.         // $sender->send('cartecadeau', ['mollivier@webapic.com'], array(), array($filepath));
  47.         return  new Response (
  48.             $dompdf->stream('resume', ["Attachment" => false]),
  49.             Response::HTTP_OK,
  50.             ['Content-Type' => 'application/pdf']
  51.         );
  52.     }
  53.     private function imageToBase64($path) {
  54.         $path $path;
  55.         $type pathinfo($pathPATHINFO_EXTENSION);
  56.         $data file_get_contents($path);
  57.         $base64 'data:image/' $type ';base64,' base64_encode($data);
  58.         return $base64;
  59.     }
  60. }