<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Dompdf\Dompdf;
use Picqer\Barcode\BarcodeGeneratorHTML;
use Sylius\Component\Mailer\Sender\SenderInterface;
class PdfGeneratorController extends AbstractController
{
public function carteCadeau(SenderInterface $sender): Response
{
$generator = new BarcodeGeneratorHTML();
$barcode = $generator->getBarcode('0000100000', $generator::TYPE_CODE_128);
$data = [
// 'logo' => $this->imageToBase64($this->getParameter('kernel.project_dir') . '/public/webapic-theme/images/logo-pdf.png'),
// 'coci' => $this->imageToBase64($this->getParameter('kernel.project_dir') . '/public/webapic-theme/images/coccinelle.png'),
'fond' => $this->imageToBase64($this->getParameter('kernel.project_dir') . '/public/webapic-theme/images/pdfcadeau-fond-barcode-date.jpg'),
'name_dest' => 'Jojo',
'name_from' => 'Juju',
'montant' => '4000',
'num' => '0000100000',
'code' => '123321',
'message' => '!!!!',
'barcode' => $barcode,
'date' => '01/01/2021'
];
$html = $this->renderView('pdf/carte-cadeau-date.html.twig', $data);
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($this->getParameter('kernel.project_dir') . '/public/media/pdf/cartecadeau.pdf', $output);
// $pdf = new Response (
// $dompdf->stream('resume', ["Attachment" => true]),
// Response::HTTP_OK,
// ['Content-Type' => 'application/pdf']
// );
$filepath = $this->getParameter('kernel.project_dir') . '/public/media/pdf/cartecadeau.pdf';
// $sender->send('cartecadeau', ['mollivier@webapic.com'], array(), array($filepath));
return new Response (
$dompdf->stream('resume', ["Attachment" => false]),
Response::HTTP_OK,
['Content-Type' => 'application/pdf']
);
}
private function imageToBase64($path) {
$path = $path;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
return $base64;
}
}