1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\DompdfBundle\Wrapper; |
13
|
|
|
|
14
|
|
|
use Core23\DompdfBundle\DompdfEvents; |
15
|
|
|
use Core23\DompdfBundle\Event\OutputEvent; |
16
|
|
|
use Core23\DompdfBundle\Event\StreamEvent; |
17
|
|
|
use Core23\DompdfBundle\Exception\PdfException; |
18
|
|
|
use Core23\DompdfBundle\Factory\DompdfFactoryInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
20
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
|
22
|
|
|
final class DompdfWrapper implements DompdfWrapperInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var DompdfFactoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $dompdfFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface|null |
31
|
|
|
*/ |
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
|
|
public function __construct(DompdfFactoryInterface $dompdfFactory, EventDispatcherInterface $eventDispatcher = null) |
35
|
|
|
{ |
36
|
|
|
$this->dompdfFactory = $dompdfFactory; |
37
|
|
|
$this->eventDispatcher = $eventDispatcher; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function streamHtml(string $html, string $filename, array $options = []): void |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$pdf = $this->dompdfFactory->create($options); |
43
|
|
|
$pdf->loadHtml($html); |
44
|
|
|
$pdf->render(); |
45
|
|
|
|
46
|
|
|
if ($this->eventDispatcher instanceof EventDispatcherInterface) { |
47
|
|
|
$event = new StreamEvent($pdf, $filename, $html); |
48
|
|
|
$this->eventDispatcher->dispatch($event, DompdfEvents::STREAM); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$pdf->stream($filename, $options); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getStreamResponse(string $html, string $filename, array $options = []): StreamedResponse |
55
|
|
|
{ |
56
|
|
|
$response = new StreamedResponse(); |
57
|
|
|
$response->setCallback(function () use ($html, $filename, $options): void { |
58
|
|
|
$this->streamHtml($html, $filename, $options); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function getPdf(string $html, array $options = []): string |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$pdf = $this->dompdfFactory->create($options); |
67
|
|
|
$pdf->loadHtml($html); |
68
|
|
|
$pdf->render(); |
69
|
|
|
|
70
|
|
|
if ($this->eventDispatcher instanceof EventDispatcherInterface) { |
71
|
|
|
$event = new OutputEvent($pdf, $html); |
72
|
|
|
$this->eventDispatcher->dispatch($event, DompdfEvents::OUTPUT); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$out = $pdf->output(); |
76
|
|
|
|
77
|
|
|
if (null === $out) { |
78
|
|
|
throw new PdfException('Error creating PDF document'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $out; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.