소스 검색

POC Remplacer wkhtmltox par librairies PHP

maha bouchiba 1 년 전
부모
커밋
1ba973b901
4개의 변경된 파일59개의 추가작업 그리고 30개의 파일을 삭제
  1. 2 1
      composer.json
  2. 7 0
      config/services.yaml
  3. 49 28
      src/Service/Export/Encoder/PdfEncoder.php
  4. 1 1
      templates/export/licence_cmf.html.twig

+ 2 - 1
composer.json

@@ -113,9 +113,10 @@
       "App\\": "src/"
     },
     "classmap": [
+      "vendor/opentalent/phpdocx/Classes/Phpdocx/Create/CreateDocument.php",
       "vendor/opentalent/phpdocx/Classes/Phpdocx/Create/CreateDocx.php",
       "vendor/opentalent/phpdocx/Classes/Phpdocx/Create/CreateDocxFromTemplate.php"
-    ]
+  ]
   },
   "autoload-dev": {
     "psr-4": {

+ 7 - 0
config/services.yaml

@@ -48,6 +48,13 @@ services:
     App\Service\Network\Utils:
         public: true
 
+    Phpdocx\Create\CreateDocx:
+        class: Phpdocx\Create\CreateDocx
+
+    App\Service\Export\Encoder\PdfEncoder:
+        arguments:
+            $phpDocx: '@Phpdocx\Create\CreateDocx'
+
     App\Service\Organization\Utils:
         public: true
 

+ 49 - 28
src/Service/Export/Encoder/PdfEncoder.php

@@ -5,32 +5,30 @@ declare(strict_types=1);
 namespace App\Service\Export\Encoder;
 
 use App\Enum\Export\ExportFormatEnum;
-use Knp\Snappy\Pdf;
+use Phpdocx\Create\CreateDocx;
+use Psr\Log\LoggerInterface;
 
 /**
  * Encode HTML to PDF.
  */
 class PdfEncoder implements EncoderInterface
 {
-    /**
-     * Default encoding options.
-     *
-     * @see https://wkhtmltopdf.org/libwkhtmltox/
-     *
-     * @var array<mixed>
-     */
     private array $defaultOptions = [
-        'margin-top' => 35,
-        'margin-right' => 10,
-        'margin-bottom' => 15,
-        'margin-left' => 15,
-        'header-spacing' => 5,
-        'enable-local-file-access' => true,
+        'marginTop' => 35,
+        'marginRight' => 10,
+        'marginBottom' => 15,
+        'marginLeft' => 15,
+        'headerSpacing' => 5,
+        'enableLocalFileAccess' => true,
     ];
 
+    private LoggerInterface $logger;
+
     public function __construct(
-        private readonly Pdf $knpSnappy
+        private readonly CreateDocx $phpDocx,
+        LoggerInterface $logger
     ) {
+        $this->logger = $logger;
     }
 
     public function support(string $format): bool
@@ -38,26 +36,49 @@ class PdfEncoder implements EncoderInterface
         return $format === ExportFormatEnum::PDF->value;
     }
 
-    /**
-     * Default encoding options.
-     *
-     * @return array<mixed>
-     */
-    public function getDefaultOptions()
+    public function getDefaultOptions(): array
     {
         return $this->defaultOptions;
     }
 
-    /**
-     * Encode the given HTML content into PDF, and
-     * return the encoded content.
-     *
-     * @param array<mixed> $options
-     */
     public function encode(string $html, array $options = []): string
     {
         $options = array_merge($this->getDefaultOptions(), $options);
 
-        return $this->knpSnappy->getOutputFromHtml($html, $options);
+        $this->phpDocx->createDocx();
+        $this->phpDocx->embedHTML($html);
+
+        $tempDir = sys_get_temp_dir();
+        $this->logger->info('Répertoire temporaire utilisé : ' . $tempDir);
+        
+        $pdfPath = tempnam($tempDir, 'pdf_') . '.pdf';
+
+        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.');
+        }
+
+        $this->logger->info('Fichier PDF temporaire créé : ' . $pdfPath);
+
+        $result = $this->phpDocx->transformDocxToPdf($pdfPath);
+
+        if ($result === false) {
+            $this->logger->error('Échec de la conversion DOCX en PDF.');
+            throw new \RuntimeException('Échec de la conversion DOCX en PDF.');
+        }
+
+        $pdfContent = file_get_contents($pdfPath);
+
+        if ($pdfContent === false) {
+            $this->logger->error('Impossible de lire le contenu du PDF.');
+            throw new \RuntimeException('Impossible de lire le contenu du PDF.');
+        }
+
+        if (!unlink($pdfPath)) {
+            $this->logger->error('Impossible de supprimer le fichier PDF temporaire.');
+            throw new \RuntimeException('Impossible de supprimer le fichier PDF temporaire.');
+        }
+
+        return $pdfContent;
     }
 }

+ 1 - 1
templates/export/licence_cmf.html.twig

@@ -192,7 +192,7 @@
                 <tr>
                     <td>
                         <p class="Style7">
-                            {% if licence.personGender %}{{ (licence.personGender ~ '_LONG') | trans }}{% endif %}
+                           {% if licence.personGender %}{{ (licence.personGender.value ~ '_LONG') | trans }}{% endif %}
                             {{ licence.personLastName }} {{ licence.personFirstName }}
                             ,</p>