Browse Source

Add dompdf chroot and pdf directory parameters in services.yaml, update FactureService

pull/5/head
ScytAl3 5 months ago
parent
commit
fbbf5a1966
  1. 10
      config/services.yaml
  2. 8
      src/Service/Facture/FactureService.php

10
config/services.yaml

@ -4,6 +4,10 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
# Directory in which the contact uploaded file should be stored
pdf_directory: "%kernel.project_dir%/public/uploads/factures/"
# Directory src for dompdf
dompdf_chroot: "%kernel.project_dir%/public/"
services:
# default configuration for services in *this* file
@ -29,3 +33,9 @@ services:
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
# service for PDF generator and storage
App\Service\Facture\FactureService:
arguments:
$pdfDirectory: "%pdf_directory%"
$chrootDirectory: "%dompdf_chroot%"

8
src/Service/Facture/FactureService.php

@ -18,6 +18,7 @@ class FactureService
protected $factureRepository;
protected $twig;
protected $pdfDirectory;
protected $chrootDirectory;
/**
* methode d ajout de dependances a la methode __construct aka injection de dependance
@ -25,12 +26,14 @@ class FactureService
* @param FactureRepository le repository pour pouvoir appeller la methode find()
* @param Environment pour travailler avec un template twig
* @param String le chemin du dossier pour enregistrer les factures generees define in config/services.yaml
* @param String $chrootDirectory : le chemin vers le dossier chroot
*/
public function __construct(FactureRepository $factureRepository, Environment $twig, string $pdfDirectory)
public function __construct(FactureRepository $factureRepository, Environment $twig, string $pdfDirectory, string $chrootDirectory)
{
$this->factureRepository = $factureRepository;
$this->twig = $twig;
$this->pdfDirectory = $pdfDirectory;
$this->chrootDirectory = $chrootDirectory;
}
/**
@ -46,6 +49,7 @@ class FactureService
//
// configure Dompdf according to the needs
$pdfOptions = new Options();
$pdfOptions->set('chroot', $this->chrootDirectory);
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsHtml5ParserEnabled(true);
$pdfOptions->setDpi(150);
@ -76,7 +80,7 @@ class FactureService
// write the file in the public directory set in config/services.yaml
$publicDirectory = $this->pdfDirectory;
// concatenate the name with the facture id
$pdfFilepath = $publicDirectory . '/facture_' . $facture->getId() . '.pdf';
$pdfFilepath = $publicDirectory . 'facture_' . $facture->getId() . '.pdf';
// write file to the desired path
file_put_contents($pdfFilepath, $output);

Loading…
Cancel
Save