1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TarfinLabs\EasyPdf; |
4
|
|
|
|
5
|
|
|
use TCPDF; |
6
|
|
|
use TCPDF_FONTS; |
7
|
|
|
|
8
|
|
|
class EasyPdf |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Tcpdf instance. |
12
|
|
|
* |
13
|
|
|
* @var TCPDF |
14
|
|
|
*/ |
15
|
|
|
protected $pdf; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Pdf header data. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $header = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Pdf footer data. |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $footer = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Barcode style. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $style = [ |
37
|
|
|
'border' => false, |
38
|
|
|
'padding' => 0, |
39
|
|
|
'fgcolor' => [0, 0, 0], |
40
|
|
|
'bgcolor' => false, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* EasyPdf constructor. |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
$this->pdf = new TCPDF(); |
49
|
|
|
$this->pdf->setPrintHeader($this->header); |
50
|
|
|
$this->pdf->setPrintFooter($this->footer); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add an array of information to the pdf instance. |
55
|
|
|
* |
56
|
|
|
* @param array $information |
57
|
|
|
* @return EasyPdf |
58
|
|
|
*/ |
59
|
|
|
public function withInformation(array $information) |
60
|
|
|
{ |
61
|
|
|
foreach ($information as $key => $value) { |
62
|
|
|
$this->setInformation($key, $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set an information on the pdf instance. |
70
|
|
|
* |
71
|
|
|
* @param $information |
72
|
|
|
* @param $value |
73
|
|
|
* @return EasyPdf |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
protected function setInformation($information, $value) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$fn = 'Set'.$information; |
78
|
|
|
|
79
|
|
|
if (is_array($value)) { |
80
|
|
|
call_user_func([$this->pdf, $fn], ...$value); |
81
|
|
|
} else { |
82
|
|
|
call_user_func([$this->pdf, $fn], $value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add an array of configurations on the pdf instance. |
90
|
|
|
* |
91
|
|
|
* @param array $config |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function withConfig(array $config) |
95
|
|
|
{ |
96
|
|
|
foreach ($config as $key => $value) { |
97
|
|
|
$this->setConfig($key, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set a configuration on the pdf instance. |
105
|
|
|
* |
106
|
|
|
* @param $config |
107
|
|
|
* @param $value |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
protected function setConfig($config, $value) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$fn = 'set'.$config; |
113
|
|
|
|
114
|
|
|
if (is_array($value)) { |
115
|
|
|
call_user_func([$this->pdf, $fn], ...$value); |
116
|
|
|
} else { |
117
|
|
|
call_user_func([$this->pdf, $fn], $value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add custom font on the pdf instance. |
125
|
|
|
* |
126
|
|
|
* @param string $font |
127
|
|
|
* @param int|null $size |
128
|
|
|
* @return EasyPdf |
129
|
|
|
*/ |
130
|
|
|
public function addFont(string $font, int $size = null) |
131
|
|
|
{ |
132
|
|
|
$tcpdfFont = TCPDF_FONTS::addTTFfont($font); |
133
|
|
|
|
134
|
|
|
$this->pdf->SetFont($tcpdfFont, '', $size); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set given font as default font. |
141
|
|
|
* |
142
|
|
|
* @param string $font |
143
|
|
|
* @param int|null $size |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function setFont(string $font, int $size = null) |
147
|
|
|
{ |
148
|
|
|
$this->pdf->SetFont($font, '', $size); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Write html content. |
155
|
|
|
* |
156
|
|
|
* @param $html |
157
|
|
|
* @return EasyPdf |
158
|
|
|
*/ |
159
|
|
|
public function loadHtml($html) |
160
|
|
|
{ |
161
|
|
|
$this->pdf->AddPage(); |
162
|
|
|
$this->pdf->writeHTML($html); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Add image to the pdf with given position and size. |
169
|
|
|
* |
170
|
|
|
* @param $image |
171
|
|
|
* @param $x |
172
|
|
|
* @param $y |
173
|
|
|
* @param $width |
174
|
|
|
* @param $height |
175
|
|
|
* @return EasyPdf |
176
|
|
|
*/ |
177
|
|
|
public function addImage($image, $x, $y, $width, $height) |
178
|
|
|
{ |
179
|
|
|
$this->pdf->Image($image, $x, $y, $width, $height); |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Override default style for barcode. |
186
|
|
|
* |
187
|
|
|
* @param array $style |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function setBarcodeStyle(array $style) |
191
|
|
|
{ |
192
|
|
|
$this->style = $style; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Add qrcode with best error correction to the pdf with given position and size. |
199
|
|
|
* |
200
|
|
|
* @param $text |
201
|
|
|
* @param $x |
202
|
|
|
* @param $y |
203
|
|
|
* @param $width |
204
|
|
|
* @param $height |
205
|
|
|
* @param string $position |
206
|
|
|
* @return EasyPdf |
207
|
|
|
*/ |
208
|
|
|
public function addQrcode($text, $x, $y, $width, $height, $position = 'N') |
209
|
|
|
{ |
210
|
|
|
$this->pdf->write2DBarcode($text, 'QRCODE,H', $x, $y, $width, $height, $this->style, $position); |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Add RAW2 barcode to the pdf with given position and size. |
217
|
|
|
* |
218
|
|
|
* @param $code |
219
|
|
|
* @param $x |
220
|
|
|
* @param $y |
221
|
|
|
* @param $width |
222
|
|
|
* @param $height |
223
|
|
|
* @param string $position |
224
|
|
|
* @return EasyPdf |
225
|
|
|
*/ |
226
|
|
|
public function addBarcode($code, $x, $y, $width, $height, $position = 'N') |
227
|
|
|
{ |
228
|
|
|
$this->pdf->write2DBarcode($code, 'RAW2', $x, $y, $width, $height, $this->style, $position); |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Set Tcpdf print header. |
235
|
|
|
* |
236
|
|
|
* @param null $header |
237
|
|
|
*/ |
238
|
|
|
public function setHeader($header = null) |
239
|
|
|
{ |
240
|
|
|
if (! is_null($header)) { |
241
|
|
|
$this->header = $header; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$this->pdf->setPrintHeader($this->header); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set Tcpdf print footer. |
249
|
|
|
* |
250
|
|
|
* @param null $footer |
251
|
|
|
*/ |
252
|
|
|
public function setFooter($footer = null) |
253
|
|
|
{ |
254
|
|
|
if (! is_null($footer)) { |
255
|
|
|
$this->footer = $footer; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$this->pdf->setPrintFooter($this->footer); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Save pdf to given path.. |
263
|
|
|
* |
264
|
|
|
* @param string $filename |
265
|
|
|
* @return mixed |
266
|
|
|
*/ |
267
|
|
|
public function save(string $filename) |
268
|
|
|
{ |
269
|
|
|
$this->pdf->Output($filename, 'F'); |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Output the generated PDF to browser. |
276
|
|
|
* |
277
|
|
|
* @return $this |
278
|
|
|
*/ |
279
|
|
|
public function stream() |
280
|
|
|
{ |
281
|
|
|
$this->pdf->Output('doc.pdf', 'I'); |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Return pdf as a string. |
288
|
|
|
* |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
public function content() |
292
|
|
|
{ |
293
|
|
|
return $this->pdf->Output('doc.pdf', 'S'); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Create a new TCPDF instance |
298
|
|
|
* |
299
|
|
|
* @return $this |
300
|
|
|
*/ |
301
|
|
|
public function reset() |
302
|
|
|
{ |
303
|
|
|
$this->pdf = new TCPDF(); |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Create a new Merge instance. |
310
|
|
|
* |
311
|
|
|
* @param array $files |
312
|
|
|
* @return Merge |
313
|
|
|
*/ |
314
|
|
|
public static function merge(array $files) |
315
|
|
|
{ |
316
|
|
|
return new Merge($files); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Create a new Parser instance. |
321
|
|
|
* |
322
|
|
|
* @param string $path |
323
|
|
|
* @return Parser |
324
|
|
|
*/ |
325
|
|
|
public static function parser(string $path): Parser |
326
|
|
|
{ |
327
|
|
|
return new Parser($path); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
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.