|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\DA\Common; |
|
4
|
|
|
|
|
5
|
|
|
use NFePHP\DA\Legacy\Common; |
|
6
|
|
|
|
|
7
|
|
|
class DaCommon extends Common |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
protected $debugmode; |
|
11
|
|
|
protected $orientacao = 'P'; |
|
12
|
|
|
protected $force; |
|
13
|
|
|
protected $papel = 'A4'; |
|
14
|
|
|
protected $margsup = 2; |
|
15
|
|
|
protected $margesq = 2; |
|
16
|
|
|
protected $wPrint; |
|
17
|
|
|
protected $hPrint; |
|
18
|
|
|
protected $xIni; |
|
19
|
|
|
protected $yIni; |
|
20
|
|
|
protected $maxH; |
|
21
|
|
|
protected $maxW; |
|
22
|
|
|
protected $fontePadrao = 'times'; |
|
23
|
|
|
protected $aFont = ['font' => 'times', 'size' => 8, 'style' => '']; |
|
24
|
|
|
protected $creditos; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Ativa ou desativa o modo debug |
|
28
|
|
|
* |
|
29
|
|
|
* @param bool $activate Ativa ou desativa o modo debug |
|
30
|
|
|
* |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function debugMode($activate = null) |
|
34
|
|
|
{ |
|
35
|
|
|
if (isset($activate) && is_bool($activate)) { |
|
36
|
|
|
$this->debugmode = $activate; |
|
37
|
|
|
} |
|
38
|
|
|
if ($this->debugmode) { |
|
39
|
|
|
//ativar modo debug |
|
40
|
|
|
error_reporting(E_ALL); |
|
41
|
|
|
ini_set('display_errors', 'On'); |
|
42
|
|
|
} else { |
|
43
|
|
|
//desativar modo debug |
|
44
|
|
|
error_reporting(0); |
|
45
|
|
|
ini_set('display_errors', 'Off'); |
|
46
|
|
|
} |
|
47
|
|
|
return $this->debugmode; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Renderiza o pdf e retorna como raw |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function render() |
|
56
|
|
|
{ |
|
57
|
|
|
if (empty($this->pdf)) { |
|
|
|
|
|
|
58
|
|
|
$this->monta(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
if (!$this->debugmode) { |
|
61
|
|
|
return $this->pdf->getPdf(); |
|
62
|
|
|
} |
|
63
|
|
|
throw new \Exception("Modo Debug Ativado"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add the credits to the integrator in the footer message |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $message Mensagem do integrador a ser impressa no rodapé das paginas |
|
70
|
|
|
* |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public function creditsIntegratorFooter($message = '') |
|
74
|
|
|
{ |
|
75
|
|
|
$this->creditos = trim($message); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $font |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setFontType(string $font = 'times') |
|
83
|
|
|
{ |
|
84
|
|
|
$this->aFont['font'] = $font; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Seta as margens superior e esquerda |
|
89
|
|
|
* a margem direita é igual a esquerda e |
|
90
|
|
|
* a inferior é igual a superior |
|
91
|
|
|
* @param int $margSup |
|
92
|
|
|
* @param int $margEsq |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function margins(int $margSup = null, int $margEsq = null) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->margsup = $margSup ?? 2; |
|
99
|
|
|
$this->margesq = $margEsq ?? 2; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Seta o tamanho da fonte |
|
104
|
|
|
* |
|
105
|
|
|
* @param int $size |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function setFontSize(int $size = 8) |
|
110
|
|
|
{ |
|
111
|
|
|
|
|
112
|
|
|
$this->aFont['size'] = $size; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $style |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function setFontStyle(string $style = '') |
|
120
|
|
|
{ |
|
121
|
|
|
$this->aFont['style'] = $style; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Seta a orientação |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $force |
|
128
|
|
|
* @param string $tpImp |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function setOrientationAndSize($force = null, $tpImp = null) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->orientacao = 'P'; |
|
135
|
|
|
if (!empty($force)) { |
|
136
|
|
|
$this->orientacao = $force; |
|
137
|
|
|
} elseif (!empty($tpImp)) { |
|
138
|
|
|
if ($tpImp == '2') { |
|
139
|
|
|
$this->orientacao = 'L'; |
|
140
|
|
|
} else { |
|
141
|
|
|
$this->orientacao = 'P'; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
if (strtoupper($this->papel) == 'A4') { |
|
145
|
|
|
$this->maxW = 210; |
|
146
|
|
|
$this->maxH = 297; |
|
147
|
|
|
} else { |
|
148
|
|
|
$this->papel = 'legal'; |
|
149
|
|
|
$this->maxW = 216; |
|
150
|
|
|
$this->maxH = 355; |
|
151
|
|
|
} |
|
152
|
|
|
if ($this->orientacao == 'L') { |
|
153
|
|
|
if (strtoupper($this->papel) == 'A4') { |
|
154
|
|
|
$this->maxH = 210; |
|
155
|
|
|
$this->maxW = 297; |
|
156
|
|
|
} else { |
|
157
|
|
|
$this->papel = 'legal'; |
|
158
|
|
|
$this->maxH = 216; |
|
159
|
|
|
$this->maxW = 355; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
$this->wPrint = $this->maxW - $this->margesq * 2; |
|
163
|
|
|
$this->hPrint = $this->maxH - $this->margsup - 5; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
protected function imagePNGtoJPG($original) |
|
167
|
|
|
{ |
|
168
|
|
|
$image = imagecreatefrompng($original); |
|
169
|
|
|
ob_start(); |
|
170
|
|
|
imagejpeg($image, null, 100); |
|
171
|
|
|
imagedestroy($image); |
|
172
|
|
|
$stringdata = ob_get_contents(); // read from buffer |
|
173
|
|
|
ob_end_clean(); |
|
174
|
|
|
return 'data://text/plain;base64,'.base64_encode($stringdata); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: