|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KNPLabs\Snappy\Backend\Dompdf; |
| 6 | + |
| 7 | +use ArrayAccess; |
| 8 | +use DOMDocument; |
| 9 | +use Dompdf; |
| 10 | +use KNPLabs\Snappy\Core\Backend\Adapter\DOMDocumentToPdf; |
| 11 | +use KNPLabs\Snappy\Core\Backend\Adapter\HtmlFileToPdf; |
| 12 | +use KNPLabs\Snappy\Core\Backend\Adapter\HtmlToPdf; |
| 13 | +use KNPLabs\Snappy\Core\Backend\Adapter\Reconfigurable; |
| 14 | +use KNPLabs\Snappy\Core\Backend\Options; |
| 15 | +use Psr\Http\Message\StreamFactoryInterface; |
| 16 | +use Psr\Http\Message\StreamInterface; |
| 17 | +use SplFileInfo; |
| 18 | + |
| 19 | +final readonly class DompdfAdapter implements DOMDocumentToPdf, HtmlFileToPdf, HtmlToPdf |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @use Reconfigurable<self> |
| 23 | + */ |
| 24 | + use Reconfigurable; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + DompdfFactory $factory, |
| 28 | + Options $options, |
| 29 | + private readonly StreamFactoryInterface $streamFactory |
| 30 | + ) { |
| 31 | + $this->factory = $factory; |
| 32 | + $this->options = $options; |
| 33 | + } |
| 34 | + |
| 35 | + public function generateFromDOMDocument(DOMDocument $DOMDocument): StreamInterface |
| 36 | + { |
| 37 | + $dompdf = $this->buildDompdf(); |
| 38 | + $dompdf->loadDOM($DOMDocument); |
| 39 | + |
| 40 | + return $this->createStream($dompdf); |
| 41 | + } |
| 42 | + |
| 43 | + public function generateFromHtmlFile(SplFileInfo $file): StreamInterface |
| 44 | + { |
| 45 | + $dompdf = $this->buildDompdf(); |
| 46 | + $dompdf->loadHtmlFile($file->getPath()); |
| 47 | + |
| 48 | + return $this->createStream($dompdf); |
| 49 | + } |
| 50 | + |
| 51 | + public function generateFromHtml(string $html): StreamInterface |
| 52 | + { |
| 53 | + $dompdf = $this->buildDompdf(); |
| 54 | + $dompdf->loadHtml($html); |
| 55 | + |
| 56 | + return $this->createStream($dompdf); |
| 57 | + } |
| 58 | + |
| 59 | + private function buildDompdf(): Dompdf\Dompdf |
| 60 | + { |
| 61 | + return new Dompdf\Dompdf( $this->compileConstructOptions()); |
| 62 | + } |
| 63 | + |
| 64 | + private function compileConstructOptions(): Dompdf\Options |
| 65 | + { |
| 66 | + $options = new Dompdf\Options( |
| 67 | + is_array($this->options->extraOptions['construct']) |
| 68 | + ? $this->options->extraOptions['construct'] |
| 69 | + : null |
| 70 | + ); |
| 71 | + |
| 72 | + if (null !== $this->options->pageOrientation) { |
| 73 | + $options->setDefaultPaperOrientation( |
| 74 | + $this->options->pageOrientation->value |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return $options; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array<mixed, mixed> |
| 83 | + */ |
| 84 | + private function compileOutputOptions(): array |
| 85 | + { |
| 86 | + $options = $this->options->extraOptions['output']; |
| 87 | + |
| 88 | + if (false === is_array($options)) { |
| 89 | + $options = []; |
| 90 | + } |
| 91 | + |
| 92 | + return $options; |
| 93 | + } |
| 94 | + |
| 95 | + private function createStream(Dompdf\Dompdf $dompdf): StreamInterface |
| 96 | + { |
| 97 | + $output = $dompdf->output($this->compileOutputOptions()); |
| 98 | + |
| 99 | + return $this |
| 100 | + ->streamFactory |
| 101 | + ->createStream($output ?: '') |
| 102 | + ; |
| 103 | + } |
| 104 | +} |
0 commit comments