Ver Fonte

pdf export functionnal

Olivier Massot há 1 ano atrás
pai
commit
ce91456636

+ 1 - 0
composer.json

@@ -23,6 +23,7 @@
     "doctrine/doctrine-bundle": "^2.1",
     "doctrine/doctrine-migrations-bundle": "^3.0",
     "doctrine/orm": "^2.17",
+    "dompdf/dompdf": "^3.0",
     "egulias/email-validator": "^3.0",
     "jbouzekri/phumbor-bundle": "^3.1.0",
     "knplabs/knp-gaufrette-bundle": "^0.8.0",

+ 2 - 2
config/packages/knp_gaufrette.yaml

@@ -4,10 +4,10 @@ knp_gaufrette:
   adapters:
     storage:
       local:
-        directory: '%kernel.project_dir%/storage'
+        directory: '%kernel.project_dir%/var/files/storage'
         create: true
   filesystems:
     storage:
       adapter: storage
 
-  stream_wrapper: ~
+  stream_wrapper: ~

+ 32 - 46
src/Service/Export/Encoder/PdfEncoder.php

@@ -5,78 +5,64 @@ declare(strict_types=1);
 namespace App\Service\Export\Encoder;
 
 use App\Enum\Export\ExportFormatEnum;
+use App\Service\Utils\Path;
+use Dompdf\Dompdf;
+use Dompdf\Options;
 use Phpdocx\Create\CreateDocx;
-use Psr\Log\LoggerInterface;
 
 /**
  * Encode HTML to PDF.
  */
 class PdfEncoder implements EncoderInterface
 {
-    private array $defaultOptions = [
-        'marginTop' => 35,
-        'marginRight' => 10,
-        'marginBottom' => 15,
-        'marginLeft' => 15,
-        'headerSpacing' => 5,
-        'enableLocalFileAccess' => true,
-    ];
-
-    private LoggerInterface $logger;
-
     public function __construct(
-        private readonly CreateDocx $phpDocx,
-        LoggerInterface $logger
-    ) {
-        $this->logger = $logger;
-    }
+        private readonly CreateDocx $phpDocx
+    ) {}
 
     public function support(string $format): bool
     {
         return $format === ExportFormatEnum::PDF->value;
     }
 
-    public function getDefaultOptions(): array
-    {
-        return $this->defaultOptions;
-    }
-
     public function encode(string $html, array $options = []): string
     {
-        $options = array_merge($this->getDefaultOptions(), $options);
-
-        $this->phpDocx->createDocx();
-        $this->phpDocx->embedHTML($html);
+        $tempDir = Path::getProjectDir() . '/var/tmp';
+        if (!is_dir($tempDir)) {
+            mkdir($tempDir);
+        }
 
-        $tempDir = sys_get_temp_dir();
-        $this->logger->info('Répertoire temporaire utilisé : ' . $tempDir);
-        
-        $pdfPath = tempnam($tempDir, 'pdf_') . '.pdf';
+        $tempFileName = $tempDir . '/' . uniqid();
 
-        if ($pdfPath === false) {
-            $this->logger->error('Impossible de créer un fichier temporaire dans le répertoire : ' . $tempDir);
-            throw new \RuntimeException('Impossible de créer un fichier temporaire.');
-        }
+        $docxFileName = $tempFileName . '.docx';
+        $pfdFileName = $tempFileName . '.pdf';
 
-        $this->logger->info('Fichier PDF temporaire créé : ' . $pdfPath);
+        // @see https://www.phpdocx.com/documentation/introduction/html-to-word-PHP#
+        $this->phpDocx->embedHTML($html);
+        $this->phpDocx->createDocx($docxFileName);
 
-        $result = $this->phpDocx->transformDocxToPdf($pdfPath);
+        $options = new Options();
+        $options->setDefaultFont('DejaVu Sans Bold');
 
-        if ($result === false) {
-            $this->logger->error('Échec de la conversion DOCX en PDF.');
-            throw new \RuntimeException('Échec de la conversion DOCX en PDF.');
-        }
+        $dompdf = new Dompdf($options);
 
-        $pdfContent = file_get_contents($pdfPath);
+        // @see https://www.phpdocx.com/documentation/introduction/pdf-conversion-plugin-installation-guide
+        $this->phpDocx->transformDocument(
+            $docxFileName,
+            $pfdFileName,
+            'native',
+            ['dompdf' => $dompdf]
+        );
 
+        $pdfContent = file_get_contents($pfdFileName);
         if ($pdfContent === false) {
-            $this->logger->error('Impossible de lire le contenu du PDF.');
-            throw new \RuntimeException('Impossible de lire le contenu du PDF.');
+            throw new \RuntimeException('Pdf file can not be read.');
         }
 
-        if (!unlink($pdfPath)) {
-            $this->logger->error('Impossible de supprimer le fichier PDF temporaire.');
-            throw new \RuntimeException('Impossible de supprimer le fichier PDF temporaire.');
+        if (!unlink($docxFileName)) {
+            throw new \RuntimeException('Temp file can not be deleted.');
+        }
+        if (!unlink($pfdFileName)) {
+            throw new \RuntimeException('Temp file can not be deleted.');
         }
 
         return $pdfContent;