2
2
3
3
namespace Barryvdh \DomPDF ;
4
4
5
+ use Dompdf \Adapter \CPDF ;
5
6
use Dompdf \Dompdf ;
6
7
use Dompdf \Options ;
7
8
use Exception ;
@@ -31,8 +32,13 @@ class PDF
31
32
/** @var \Illuminate\Contracts\View\Factory */
32
33
protected $ view ;
33
34
35
+ /** @var bool */
34
36
protected $ rendered = false ;
37
+
38
+ /** @var bool */
35
39
protected $ showWarnings ;
40
+
41
+ /** @var string */
36
42
protected $ public_path ;
37
43
38
44
/**
@@ -56,31 +62,26 @@ public function __construct(Dompdf $dompdf, ConfigRepository $config, Filesystem
56
62
*
57
63
* @return Dompdf
58
64
*/
59
- public function getDomPDF ()
65
+ public function getDomPDF (): Dompdf
60
66
{
61
67
return $ this ->dompdf ;
62
68
}
63
69
64
70
/**
65
71
* Set the paper size (default A4)
66
72
*
67
- * @param string|array $paper
68
- * @param string $orientation
69
- * @return $this
73
+ * @param string|array<string> $paper
70
74
*/
71
- public function setPaper ($ paper , $ orientation = 'portrait ' )
75
+ public function setPaper ($ paper , string $ orientation = 'portrait ' ): self
72
76
{
73
77
$ this ->dompdf ->setPaper ($ paper , $ orientation );
74
78
return $ this ;
75
79
}
76
80
77
81
/**
78
82
* Show or hide warnings
79
- *
80
- * @param bool $warnings
81
- * @return $this
82
83
*/
83
- public function setWarnings ($ warnings )
84
+ public function setWarnings (bool $ warnings ): self
84
85
{
85
86
$ this ->showWarnings = $ warnings ;
86
87
return $ this ;
@@ -89,11 +90,9 @@ public function setWarnings($warnings)
89
90
/**
90
91
* Load a HTML string
91
92
*
92
- * @param string $string
93
- * @param string $encoding Not used yet
94
- * @return static
93
+ * @param string|null $encoding Not used yet
95
94
*/
96
- public function loadHTML ($ string , $ encoding = null )
95
+ public function loadHTML (string $ string , ? string $ encoding = null ): self
97
96
{
98
97
$ string = $ this ->convertEntities ($ string );
99
98
$ this ->dompdf ->loadHtml ($ string , $ encoding );
@@ -103,11 +102,8 @@ public function loadHTML($string, $encoding = null)
103
102
104
103
/**
105
104
* Load a HTML file
106
- *
107
- * @param string $file
108
- * @return static
109
105
*/
110
- public function loadFile ($ file )
106
+ public function loadFile (string $ file ): self
111
107
{
112
108
$ this ->dompdf ->loadHtmlFile ($ file );
113
109
$ this ->rendered = false ;
@@ -116,11 +112,10 @@ public function loadFile($file)
116
112
117
113
/**
118
114
* Add metadata info
119
- *
120
- * @param array $info
115
+ * @param array<string, string> $info
121
116
* @return static
122
117
*/
123
- public function addInfo ($ info )
118
+ public function addInfo (array $ info ): self
124
119
{
125
120
foreach ($ info as $ name => $ value ) {
126
121
$ this ->dompdf ->add_info ($ name , $ value );
@@ -130,14 +125,11 @@ public function addInfo($info)
130
125
131
126
/**
132
127
* Load a View and convert to HTML
133
- *
134
- * @param string $view
135
- * @param array $data
136
- * @param array $mergeData
137
- * @param string $encoding Not used yet
138
- * @return static
128
+ * @param array<string, mixed> $data
129
+ * @param array<string, mixed> $mergeData
130
+ * @param string|null $encoding Not used yet
139
131
*/
140
- public function loadView ($ view , $ data = array (), $ mergeData = array (), $ encoding = null )
132
+ public function loadView (string $ view , array $ data = [], array $ mergeData = [], ? string $ encoding = null ): self
141
133
{
142
134
$ html = $ this ->view ->make ($ view , $ data , $ mergeData )->render ();
143
135
return $ this ->loadHTML ($ html , $ encoding );
@@ -146,10 +138,9 @@ public function loadView($view, $data = array(), $mergeData = array(), $encoding
146
138
/**
147
139
* Set/Change an option in DomPdf
148
140
*
149
- * @param array $options
150
- * @return static
141
+ * @param array<string, mixed> $options
151
142
*/
152
- public function setOptions (array $ options )
143
+ public function setOptions (array $ options ): self
153
144
{
154
145
$ options = new Options ($ options );
155
146
$ this ->dompdf ->setOptions ($ options );
@@ -164,70 +155,57 @@ public function setOptions(array $options)
164
155
* 'compress' = > 1 or 0 - apply content stream compression, this is
165
156
* on (1) by default
166
157
*
167
- * @param array $options
158
+ * @param array<string, int> $options
168
159
*
169
160
* @return string The rendered PDF as string
170
161
*/
171
- public function output ($ options = [])
162
+ public function output (array $ options = []): string
172
163
{
173
164
if (!$ this ->rendered ) {
174
165
$ this ->render ();
175
166
}
176
- return $ this ->dompdf ->output ($ options );
167
+ return ( string ) $ this ->dompdf ->output ($ options );
177
168
}
178
169
179
170
/**
180
171
* Save the PDF to a file
181
- *
182
- * @param $filename
183
- * @return static
184
172
*/
185
- public function save ($ filename )
173
+ public function save (string $ filename ): self
186
174
{
187
175
$ this ->files ->put ($ filename , $ this ->output ());
188
176
return $ this ;
189
177
}
190
178
191
179
/**
192
180
* Make the PDF downloadable by the user
193
- *
194
- * @param string $filename
195
- * @return \Illuminate\Http\Response
196
181
*/
197
- public function download ($ filename = 'document.pdf ' )
182
+ public function download (string $ filename = 'document.pdf ' ): Response
198
183
{
199
184
$ output = $ this ->output ();
200
- return new Response ($ output , 200 , array (
201
- 'Content-Type ' => 'application/pdf ' ,
202
- 'Content-Disposition ' => 'attachment; filename=" ' . $ filename . '" ' ,
203
- 'Content-Length ' => strlen ($ output ),
204
- ) );
185
+ return new Response ($ output , 200 , [
186
+ 'Content-Type ' => 'application/pdf ' ,
187
+ 'Content-Disposition ' => 'attachment; filename=" ' . $ filename . '" ' ,
188
+ 'Content-Length ' => strlen ($ output ),
189
+ ] );
205
190
}
206
191
207
192
/**
208
193
* Return a response with the PDF to show in the browser
209
- *
210
- * @param string $filename
211
- * @return \Illuminate\Http\Response
212
194
*/
213
- public function stream ($ filename = 'document.pdf ' )
195
+ public function stream (string $ filename = 'document.pdf ' ): Response
214
196
{
215
197
$ output = $ this ->output ();
216
- return new Response ($ output , 200 , array (
198
+ return new Response ($ output , 200 , [
217
199
'Content-Type ' => 'application/pdf ' ,
218
200
'Content-Disposition ' => 'inline; filename=" ' . $ filename . '" ' ,
219
- ) );
201
+ ] );
220
202
}
221
203
222
204
/**
223
205
* Render the PDF
224
206
*/
225
- public function render ()
207
+ public function render (): void
226
208
{
227
- if (!$ this ->dompdf ) {
228
- throw new Exception ('DOMPDF not created yet ' );
229
- }
230
-
231
209
$ this ->dompdf ->render ();
232
210
233
211
if ($ this ->showWarnings ) {
@@ -246,25 +224,27 @@ public function render()
246
224
$ this ->rendered = true ;
247
225
}
248
226
249
- public function setEncryption ($ password , $ ownerpassword = '' , $ pc = [])
227
+ /** @param array<string> $pc */
228
+ public function setEncryption (string $ password , string $ ownerpassword = '' , array $ pc = []): void
250
229
{
251
- if (!$ this ->dompdf ) {
252
- throw new Exception ("DOMPDF not created yet " );
253
- }
254
- $ this ->render ();
255
- return $ this ->dompdf ->getCanvas ()->get_cpdf ()->setEncryption ($ password , $ ownerpassword , $ pc );
230
+ $ this ->render ();
231
+ $ canvas = $ this ->dompdf ->getCanvas ();
232
+ if (! $ canvas instanceof CPDF ) {
233
+ throw new \RuntimeException ('Encryption is only supported when using CPDF ' );
234
+ }
235
+ $ canvas ->get_cpdf ()->setEncryption ($ password , $ ownerpassword , $ pc );
256
236
}
257
237
258
- protected function convertEntities ($ subject )
238
+ protected function convertEntities (string $ subject ): string
259
239
{
260
240
if (false === $ this ->config ->get ('dompdf.convert_entities ' , true )) {
261
241
return $ subject ;
262
242
}
263
243
264
- $ entities = array (
244
+ $ entities = [
265
245
'€ ' => '€ ' ,
266
246
'£ ' => '£ ' ,
267
- ) ;
247
+ ] ;
268
248
269
249
foreach ($ entities as $ search => $ replace ) {
270
250
$ subject = str_replace ($ search , $ replace , $ subject );
0 commit comments