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\Events; |
15
|
|
|
use Core23\DompdfBundle\Factory\DompdfFactoryInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
18
|
|
|
|
19
|
|
|
final class DompdfWrapper implements DompdfWrapperInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DompdfFactoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $dompdfFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EventDispatcherInterface |
28
|
|
|
*/ |
29
|
|
|
private $eventDispatcher; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DompdfFactoryInterface $dompdfFactory |
33
|
|
|
*/ |
34
|
|
|
public function __construct(DompdfFactoryInterface $dompdfFactory) |
35
|
|
|
{ |
36
|
|
|
$this->dompdfFactory = $dompdfFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the event dispatcher. |
41
|
|
|
* |
42
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
43
|
|
|
*/ |
44
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
45
|
|
|
{ |
46
|
|
|
$this->eventDispatcher = $eventDispatcher; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function streamHtml(string $html, string $filename, array $options = []): void |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$pdf = $this->dompdfFactory->create($options); |
55
|
|
|
$pdf->loadHtml($html); |
56
|
|
|
$pdf->render(); |
57
|
|
|
|
58
|
|
|
if ($this->eventDispatcher instanceof EventDispatcherInterface) { |
59
|
|
|
$event = new GenericEvent($pdf, ['filename' => $filename, 'html' => $html]); |
60
|
|
|
$this->eventDispatcher->dispatch(Events::STREAM, $event); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$pdf->stream($filename); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function getPdf(string $html, array $options = []): string |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$pdf = $this->dompdfFactory->create($options); |
72
|
|
|
$pdf->loadHtml($html); |
73
|
|
|
$pdf->render(); |
74
|
|
|
|
75
|
|
|
if ($this->eventDispatcher instanceof EventDispatcherInterface) { |
76
|
|
|
$event = new GenericEvent($pdf, ['html' => $html]); |
77
|
|
|
$this->eventDispatcher->dispatch(Events::OUTPUT, $event); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $pdf->output(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.