|
|
@@ -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;
|
|
|
}
|
|
|
}
|