1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine PDF |
5
|
|
|
* |
6
|
|
|
* Platine PDF is the lightweight for generating PDF documents |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine PDF |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file DOMPDFGenerator.php |
33
|
|
|
* |
34
|
|
|
* The DOMPDF generator class |
35
|
|
|
* |
36
|
|
|
* @package Platine\PDF\Generator |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link http://www.iacademy.cf |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\PDF\Generator; |
48
|
|
|
|
49
|
|
|
use Dompdf\Dompdf; |
50
|
|
|
use Platine\Filesystem\Filesystem; |
51
|
|
|
use Platine\PDF\PDFGeneratorInterface; |
52
|
|
|
use RuntimeException; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @class DOMPDFGenerator |
56
|
|
|
* @package Platine\PDF\Generator |
57
|
|
|
*/ |
58
|
|
|
class DOMPDFGenerator implements PDFGeneratorInterface |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The DOMPDF instance |
63
|
|
|
* @var Dompdf |
64
|
|
|
*/ |
65
|
|
|
protected Dompdf $dompdf; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The file system to use |
69
|
|
|
* @var Filesystem |
70
|
|
|
*/ |
71
|
|
|
protected Filesystem $filesystem; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Whether the document already rendered call |
75
|
|
|
* mean to render() method |
76
|
|
|
* @var bool |
77
|
|
|
*/ |
78
|
|
|
protected $rendered = false; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The PDF generated filename |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $filename = 'dompdf.pdf'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create new instance |
88
|
|
|
* @param Dompdf $dompdf |
89
|
|
|
* @param Filesystem $filesystem |
90
|
|
|
*/ |
91
|
|
|
public function __construct(Dompdf $dompdf, Filesystem $filesystem) |
92
|
|
|
{ |
93
|
|
|
$this->dompdf = $dompdf; |
94
|
|
|
$this->filesystem = $filesystem; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the DOMPDF |
99
|
|
|
* @return Dompdf |
100
|
|
|
*/ |
101
|
|
|
public function getDompdf(): Dompdf |
102
|
|
|
{ |
103
|
|
|
return $this->dompdf; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the DOMPDF instance |
108
|
|
|
* @param Dompdf $dompdf |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setDompdf(Dompdf $dompdf): self |
112
|
|
|
{ |
113
|
|
|
$this->dompdf = $dompdf; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritodc} |
119
|
|
|
*/ |
120
|
|
|
public function generate( |
121
|
|
|
string $content, |
122
|
|
|
string $filename = 'output.pdf', |
123
|
|
|
string $format = 'A4', |
124
|
|
|
string $orientation = 'portrait' |
125
|
|
|
): void { |
126
|
|
|
$this->filename = $filename; |
127
|
|
|
$this->dompdf->loadHtml($content); |
128
|
|
|
$this->dompdf->setPaper($format, $orientation); |
129
|
|
|
$this->dompdf->render(); |
130
|
|
|
|
131
|
|
|
$this->rendered = true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritodc} |
136
|
|
|
*/ |
137
|
|
|
public function raw(): string |
138
|
|
|
{ |
139
|
|
|
$this->checkIfAlreadyRendered(); |
140
|
|
|
|
141
|
|
|
return (string) $this->dompdf->output(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritodc} |
146
|
|
|
*/ |
147
|
|
|
public function save(): void |
148
|
|
|
{ |
149
|
|
|
$this->checkIfAlreadyRendered(); |
150
|
|
|
|
151
|
|
|
$this->filesystem |
152
|
|
|
->file($this->filename) |
153
|
|
|
->write($this->raw()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritodc} |
158
|
|
|
*/ |
159
|
|
|
public function download(): void |
160
|
|
|
{ |
161
|
|
|
$this->checkIfAlreadyRendered(); |
162
|
|
|
$this->dompdf->stream($this->filename); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if the document already rendered |
167
|
|
|
* @return void |
168
|
|
|
* @throws RuntimeException |
169
|
|
|
*/ |
170
|
|
|
protected function checkIfAlreadyRendered(): void |
171
|
|
|
{ |
172
|
|
|
if (!$this->rendered) { |
173
|
|
|
throw new RuntimeException('You must render the document first'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|