Sfoglia il codice sorgente

configuration de phpdocx

Maha Bouchiba 2 anni fa
parent
commit
bcf8951ddb
2 ha cambiato i file con 40 aggiunte e 1 eliminazioni
  1. 1 1
      composer.json
  2. 39 0
      src/Service/Export/Encoder/DocXEncoder

+ 1 - 1
composer.json

@@ -156,4 +156,4 @@
       ]
     }
   }
-}
+}

+ 39 - 0
src/Service/Export/Encoder/DocXEncoder

@@ -0,0 +1,39 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service\Export\Encoder;
+
+use App\Enum\Export\ExportFormatEnum;
+use Phpdocx\Create\CreateDocx;
+
+/**
+ * Encode HTML to docx format
+ */
+class DocXEncoder implements EncoderInterface
+{
+
+  public function support(string $format): bool
+  {
+    return $format === ExportFormatEnum::DOCX()->getValue();
+  }
+
+  /**
+   * Encode the given HTML content into docX, and
+   * return the encoded content
+   *
+   * @param string $html
+   * @param array<mixed> $options
+   * @return string
+   */
+  public function encode(string $html, array $options = []): string
+  {
+    $docx = new CreateDocx();
+    $docx->addText($html);
+    $tempFile = tempnam(sys_get_temp_dir(), 'docx');
+    $docx->createDocx($tempFile);
+    $docxContent = file_get_contents($tempFile);
+    unlink($tempFile);
+    return $docxContent;
+  }
+}