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