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 https://www.platine-php.com |
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
|
|
|
* The DOMPDF instance |
62
|
|
|
* @var Dompdf |
63
|
|
|
*/ |
64
|
|
|
protected Dompdf $dompdf; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The file system to use |
68
|
|
|
* @var Filesystem |
69
|
|
|
*/ |
70
|
|
|
protected Filesystem $filesystem; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Whether the document already rendered call |
74
|
|
|
* mean to render() method |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
protected $rendered = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The PDF generated filename |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected $filename = 'dompdf.pdf'; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create new instance |
87
|
|
|
* @param Dompdf $dompdf |
88
|
|
|
* @param Filesystem $filesystem |
89
|
|
|
*/ |
90
|
|
|
public function __construct(Dompdf $dompdf, Filesystem $filesystem) |
91
|
|
|
{ |
92
|
|
|
$this->dompdf = $dompdf; |
93
|
|
|
$this->filesystem = $filesystem; |
94
|
|
|
|
95
|
|
|
// TODO |
96
|
|
|
$this->disableSslVerify(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the DOMPDF |
101
|
|
|
* @return Dompdf |
102
|
|
|
*/ |
103
|
|
|
public function getDompdf(): Dompdf |
104
|
|
|
{ |
105
|
|
|
return $this->dompdf; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the DOMPDF instance |
110
|
|
|
* @param Dompdf $dompdf |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setDompdf(Dompdf $dompdf): self |
114
|
|
|
{ |
115
|
|
|
$this->dompdf = $dompdf; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function generate( |
123
|
|
|
string $content, |
124
|
|
|
string $filename = 'output.pdf', |
125
|
|
|
string $format = 'A4', |
126
|
|
|
string $orientation = 'portrait' |
127
|
|
|
): void { |
128
|
|
|
$this->filename = $filename; |
129
|
|
|
$this->dompdf->loadHtml($content); |
130
|
|
|
$this->dompdf->setPaper($format, $orientation); |
131
|
|
|
$this->dompdf->render(); |
132
|
|
|
|
133
|
|
|
$this->rendered = true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function raw(): string |
140
|
|
|
{ |
141
|
|
|
$this->checkIfAlreadyRendered(); |
142
|
|
|
|
143
|
|
|
return (string) $this->dompdf->output(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function save(): void |
150
|
|
|
{ |
151
|
|
|
$this->checkIfAlreadyRendered(); |
152
|
|
|
|
153
|
|
|
$this->filesystem |
154
|
|
|
->file($this->filename) |
155
|
|
|
->write($this->raw()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function download(): void |
162
|
|
|
{ |
163
|
|
|
$this->checkIfAlreadyRendered(); |
164
|
|
|
$this->dompdf->stream($this->filename); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Disable the SSL verification |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function disableSslVerify(): self |
172
|
|
|
{ |
173
|
|
|
$context = stream_context_create([ |
174
|
|
|
'ssl' => [ |
175
|
|
|
'verify_peer' => false, |
176
|
|
|
'verify_peer_name' => false, |
177
|
|
|
'allow_self_signed' => true |
178
|
|
|
] |
179
|
|
|
]); |
180
|
|
|
$this->dompdf->setHttpContext($context); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Check if the document already rendered |
187
|
|
|
* @return void |
188
|
|
|
* @throws RuntimeException |
189
|
|
|
*/ |
190
|
|
|
protected function checkIfAlreadyRendered(): void |
191
|
|
|
{ |
192
|
|
|
if (!$this->rendered) { |
193
|
|
|
throw new RuntimeException('You must render the document first'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|