|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\DA\Common; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe comum para a impressão em PDF do Documento Auxiliar de Bilhete de Passagem eletronico |
|
7
|
|
|
* |
|
8
|
|
|
* @category Library |
|
9
|
|
|
* @package nfephp-org/sped-da |
|
10
|
|
|
* @copyright 2009-2020 NFePHP |
|
11
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 or MIT |
|
12
|
|
|
* @link http://github.com/nfephp-org/sped-da for the canonical source repository |
|
13
|
|
|
* @author Roberto L. Machado <linux at rlm dot gmail dot com> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
use NFePHP\DA\Legacy\Common; |
|
17
|
|
|
|
|
18
|
|
|
class DaCommon extends Common |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $debugmode; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $orientacao; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string|array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $papel = 'A4'; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $margsup = 2; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $margesq = 2; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var float |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $wPrint; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var float |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $hPrint; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var float |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $xIni; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var float |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $yIni; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var float |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $maxH; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var float |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $maxW; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $fontePadrao = 'times'; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $aFont = ['font' => 'times', 'size' => 8, 'style' => '']; |
|
72
|
|
|
/** |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $creditos; |
|
76
|
|
|
/** |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $logomarca; |
|
80
|
|
|
/** |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $logoAlign = 'L'; |
|
84
|
|
|
/** |
|
85
|
|
|
* @var \NFePHP\DA\Legacy\Pdf |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $pdf; |
|
88
|
|
|
/** |
|
89
|
|
|
* @var string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $numdepec; |
|
92
|
|
|
/** |
|
93
|
|
|
* @var int |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $decimalPlaces; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Ativa ou desativa o modo debug |
|
99
|
|
|
* @param bool $activate Ativa ou desativa o modo debug |
|
100
|
|
|
* @return bool |
|
101
|
|
|
* @throws \Exception |
|
102
|
|
|
*/ |
|
103
|
|
|
public function debugMode($activate = null) |
|
104
|
|
|
{ |
|
105
|
|
|
if (isset($activate) && is_bool($activate)) { |
|
106
|
|
|
$this->debugmode = $activate; |
|
107
|
|
|
} |
|
108
|
|
|
if ($this->debugmode) { |
|
109
|
|
|
//ativar modo debug |
|
110
|
|
|
error_reporting(E_ALL); |
|
111
|
|
|
ini_set('display_errors', 'On'); |
|
112
|
|
|
set_error_handler(function (int $errnum, string $errmsg, string $errfile, int $errline) { |
|
113
|
|
|
throw new \Exception("Erro identificado $errnum: '$errmsg' $errfile [linha:" . $errline . "]"); |
|
114
|
|
|
}); |
|
115
|
|
|
} else { |
|
116
|
|
|
//desativar modo debug |
|
117
|
|
|
error_reporting(0); |
|
118
|
|
|
ini_set('display_errors', 'Off'); |
|
119
|
|
|
} |
|
120
|
|
|
return $this->debugmode; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Define parametros de impressão |
|
125
|
|
|
* @param string $orientacao |
|
126
|
|
|
* @param string $papel |
|
127
|
|
|
* @param int $margSup |
|
128
|
|
|
* @param int $margEsq |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
|
|
public function printParameters( |
|
132
|
|
|
$orientacao = '', |
|
133
|
|
|
$papel = 'A4', |
|
134
|
|
|
$margSup = 2, |
|
135
|
|
|
$margEsq = 2 |
|
136
|
|
|
) { |
|
137
|
|
|
if ($orientacao === 'P' || $orientacao === 'L') { |
|
138
|
|
|
$this->orientacao = $orientacao; |
|
139
|
|
|
} |
|
140
|
|
|
$p = strtoupper($papel); |
|
141
|
|
|
if ($p == 'A4' || $p == 'LEGAL') { |
|
142
|
|
|
$this->papel = $papel; |
|
143
|
|
|
} |
|
144
|
|
|
$this->margsup = $margSup ?? 2; |
|
145
|
|
|
$this->margesq = $margEsq ?? 2; |
|
146
|
|
|
if (strtoupper($this->papel) == 'A4') { |
|
147
|
|
|
$this->maxW = 210; |
|
|
|
|
|
|
148
|
|
|
$this->maxH = 297; |
|
|
|
|
|
|
149
|
|
|
} else { |
|
150
|
|
|
$this->papel = 'legal'; |
|
151
|
|
|
$this->maxW = 216; |
|
|
|
|
|
|
152
|
|
|
$this->maxH = 355; |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
if ($this->orientacao == 'L') { |
|
155
|
|
|
if (strtoupper($this->papel) == 'A4') { |
|
156
|
|
|
$this->maxH = 210; |
|
|
|
|
|
|
157
|
|
|
$this->maxW = 297; |
|
|
|
|
|
|
158
|
|
|
} else { |
|
159
|
|
|
$this->papel = 'legal'; |
|
160
|
|
|
$this->maxH = 216; |
|
|
|
|
|
|
161
|
|
|
$this->maxW = 355; |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
$this->wPrint = $this->maxW - $this->margesq * 2; |
|
|
|
|
|
|
165
|
|
|
$this->hPrint = $this->maxH - $this->margsup - 5; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set logo e sua posição |
|
170
|
|
|
* @param string $logo |
|
171
|
|
|
* @param string $logoAlign |
|
172
|
|
|
* @param bool $mode_bw se true converte a imagem em branco e preto |
|
173
|
|
|
* @return void |
|
174
|
|
|
*/ |
|
175
|
|
|
public function logoParameters($logo, $logoAlign = null, $mode_bw = false) |
|
176
|
|
|
{ |
|
177
|
|
|
if (!empty($logoAlign)) { |
|
178
|
|
|
$this->logoAlign = $logoAlign; |
|
179
|
|
|
} |
|
180
|
|
|
$this->logomarca = $this->adjustImage($logo, $mode_bw); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Numero DPEC |
|
185
|
|
|
* @param string $numdepec |
|
186
|
|
|
* @return void |
|
187
|
|
|
*/ |
|
188
|
|
|
public function depecNumber($numdepec) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->numdepec = $numdepec; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Renderiza o pdf e retorna como raw |
|
195
|
|
|
* @param string $logo |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public function render( |
|
199
|
|
|
$logo = '' |
|
200
|
|
|
) { |
|
201
|
|
|
if (empty($this->pdf)) { |
|
202
|
|
|
$this->monta($logo); |
|
203
|
|
|
} |
|
204
|
|
|
return $this->pdf->getPdf(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Add the credits to the integrator in the footer message |
|
209
|
|
|
* @param string $message Mensagem do integrador a ser impressa no rodapé das paginas |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function creditsIntegratorFooter($message = '') |
|
213
|
|
|
{ |
|
214
|
|
|
$this->creditos = trim($message); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Seta a fonte padrão é times |
|
219
|
|
|
* @param string $font |
|
220
|
|
|
*/ |
|
221
|
|
|
public function setDefaultFont(string $font = 'times') |
|
222
|
|
|
{ |
|
223
|
|
|
$this->fontePadrao = $font; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Seta o numero de casas decimais a serem usadas como padrão |
|
228
|
|
|
* @param int $dec |
|
229
|
|
|
*/ |
|
230
|
|
|
public function setDefaultDecimalPlaces($dec) |
|
231
|
|
|
{ |
|
232
|
|
|
if ($dec > 4) { |
|
233
|
|
|
$dec = 4; |
|
234
|
|
|
} |
|
235
|
|
|
if ($dec < 2) { |
|
236
|
|
|
$dec = 2; |
|
237
|
|
|
} |
|
238
|
|
|
$this->decimalPlaces = $dec; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Metodo de montagem do PDF |
|
243
|
|
|
* @param string $logo |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function monta($logo = null) |
|
246
|
|
|
{ |
|
247
|
|
|
//todo replaced in other classes |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Ajusta a imagem do logo |
|
252
|
|
|
* @param string $logo |
|
253
|
|
|
* @param bool $turn_bw |
|
254
|
|
|
* @return string |
|
255
|
|
|
* @throws \Exception |
|
256
|
|
|
*/ |
|
257
|
|
|
protected function adjustImage($logo, $turn_bw = false) |
|
258
|
|
|
{ |
|
259
|
|
|
if (substr($logo, 0, 24) !== 'data://text/plain;base64') { |
|
260
|
|
|
if (is_file($logo)) { |
|
261
|
|
|
$logo = 'data://text/plain;base64,'. base64_encode(file_get_contents($logo)); |
|
262
|
|
|
} else { |
|
263
|
|
|
$logo = ''; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
$logoInfo = getimagesize($logo); |
|
267
|
|
|
//1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), |
|
268
|
|
|
//8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, |
|
269
|
|
|
//14 = IFF, 15 = WBMP, 16 = XBM |
|
270
|
|
|
$type = $logoInfo[2]; |
|
271
|
|
|
if ($type != '2' && $type != '3') { |
|
272
|
|
|
throw new \Exception('O formato da imagem não é aceitável! Somente PNG ou JPG podem ser usados.'); |
|
273
|
|
|
} |
|
274
|
|
|
if ($type == '3') { //3 = PNG |
|
275
|
|
|
$image = imagecreatefrompng($logo); |
|
276
|
|
|
if ($turn_bw) { |
|
277
|
|
|
imagefilter($image, IMG_FILTER_GRAYSCALE); |
|
278
|
|
|
//imagefilter($image, IMG_FILTER_CONTRAST, -100); |
|
279
|
|
|
} |
|
280
|
|
|
return $this->getImageStringFromObject($image); |
|
281
|
|
|
} elseif ($type == '2' && $turn_bw) { |
|
282
|
|
|
$image = imagecreatefromjpeg($logo); |
|
283
|
|
|
imagefilter($image, IMG_FILTER_GRAYSCALE); |
|
284
|
|
|
//imagefilter($image, IMG_FILTER_CONTRAST, -100); |
|
285
|
|
|
return $this->getImageStringFromObject($image); |
|
286
|
|
|
} |
|
287
|
|
|
return $logo; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Cria uma imagem JPEG com o objeto GD |
|
292
|
|
|
* @param resource $image |
|
293
|
|
|
* @return string |
|
294
|
|
|
*/ |
|
295
|
|
|
private function getImageStringFromObject($image) |
|
296
|
|
|
{ |
|
297
|
|
|
ob_start(); |
|
298
|
|
|
imagejpeg($image, null, 100); |
|
299
|
|
|
imagedestroy($image); |
|
300
|
|
|
$logo = ob_get_contents(); // read from buffer |
|
301
|
|
|
ob_end_clean(); |
|
302
|
|
|
return 'data://text/plain;base64,'.base64_encode($logo); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.