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\Factory\DompdfFactoryInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
16
|
|
|
|
17
|
|
|
final class DompdfWrapper implements DompdfWrapperInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var DompdfFactoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $dompdfFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param DompdfFactoryInterface $dompdfFactory |
26
|
|
|
*/ |
27
|
|
|
public function __construct(DompdfFactoryInterface $dompdfFactory) |
28
|
|
|
{ |
29
|
|
|
$this->dompdfFactory = $dompdfFactory; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function streamHtml(string $html, string $filename, array $options = []): void |
36
|
|
|
{ |
37
|
|
|
$pdf = $this->dompdfFactory->create($options); |
38
|
|
|
$pdf->loadHtml($html); |
39
|
|
|
$pdf->render(); |
40
|
|
|
$pdf->stream($filename, $options); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $html |
45
|
|
|
* @param string $filename |
46
|
|
|
* @param array $options |
47
|
|
|
* |
48
|
|
|
* @return StreamedResponse |
49
|
|
|
*/ |
50
|
|
|
public function getStreamResponse(string $html, string $filename, array $options = []): StreamedResponse |
51
|
|
|
{ |
52
|
|
|
$response = new StreamedResponse(); |
53
|
|
|
$response->setCallback(function () use ($html, $filename, $options): void { |
|
|
|
|
54
|
|
|
$this->streamHtml($html, $filename, $options); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
return $response; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getPdf(string $html, array $options = []): string |
64
|
|
|
{ |
65
|
|
|
$pdf = $this->dompdfFactory->create($options); |
66
|
|
|
$pdf->loadHtml($html); |
67
|
|
|
$pdf->render(); |
68
|
|
|
|
69
|
|
|
return $pdf->output(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|