浏览代码

restore docx encoder after rebase error

Olivier Massot 2 年之前
父节点
当前提交
b974f03d4a
共有 1 个文件被更改,包括 39 次插入0 次删除
  1. 39 0
      src/Service/Export/Encoder/DocXEncoder.php

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

@@ -0,0 +1,39 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service\Export\Encoder;
+
+use Phpdocx\Create\CreateDocx;
+use App\Enum\Export\ExportFormatEnum;
+
+/**
+ * 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->embedHTML($html);
+        $tempFile = tempnam(sys_get_temp_dir(), 'docx');
+        $docx->createDocx($tempFile);
+        $content = file_get_contents($tempFile);
+        unlink($tempFile);
+        return $content;
+    }
+}