| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Export\Encoder;
- use App\Enum\Export\ExportFormatEnum;
- use App\Service\Utils\DebugUtils;
- use App\Service\Utils\PathUtils;
- use Dompdf\Dompdf;
- use Dompdf\Options;
- /**
- * Encode HTML to PDF.
- */
- class PdfEncoder implements EncoderInterface
- {
- public function support(string $format): bool
- {
- return $format === ExportFormatEnum::PDF->value;
- }
- /**
- * Converts the provided HTML content into a PDF document.
- *
- * @param string $html the HTML content to be converted to PDF
- * @param array<string, mixed> $options Optional configuration settings for the PDF generation
- *
- * @see https://github.com/dompdf/dompdf/blob/master/src/Options.php
- *
- * @return string the generated PDF content as a string
- */
- public function encode(string $html, array $options = []): string
- {
- $domPdfOptions = new Options();
- $dompdf = new Dompdf();
- $domPdfOptions->setIsRemoteEnabled(true);
- $domPdfOptions->setChroot(PathUtils::getProjectDir().'/public');
- $domPdfOptions->setDefaultPaperOrientation('portrait');
- $domPdfOptions->setDefaultPaperSize('A4');
- $domPdfOptions->set($options);
- $dompdf->setOptions($domPdfOptions);
- $dompdf->loadHtml($html);
- $dompdf->render();
- return $dompdf->output();
- }
- }
|