-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescpos.class.php
executable file
·237 lines (185 loc) · 4.83 KB
/
escpos.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* ESC/POS
*
* ESC/POS print driver for PHP
* This driver has been know to work with Epson TM-T20
*
* @version 20150315
* @author Guillaume Marsay <[email protected]>
* @link https://github.com/Futurolan/escpos
*/
class ESCPOS
{
const NUL = "\x00";
const LF = "\x0a";
const ESC = "\x1b";
const GS = "\x1d";
const MODE_FONT_A = 0;
const MODE_FONT_B = 1;
const MODE_EMPHASIZED = 8;
const MODE_DOUBLE_HEIGHT = 16;
const MODE_DOUBLE_WIDTH = 32;
const MODE_UNDERLINE = 128;
const FONT_A = 0;
const FONT_B = 1;
const FONT_C = 2;
const JUSTIFY_LEFT = 0;
const JUSTIFY_CENTER = 1;
const JUSTIFY_RIGHT = 2;
const CUT_FULL = 65;
const CUT_PARTIAL = 66;
const BARCODE_UPCA = 0;
const BARCODE_UPCE = 1;
const BARCODE_JAN13 = 2;
const BARCODE_JAN8 = 3;
const BARCODE_CODE39 = 4;
const BARCODE_ITF = 5;
const BARCODE_CODABAR = 6;
private $fp;
function __construct($fp = null)
{
if (is_null($fp))
{
$fp = fopen("php://stdout", "wb");
}
$this->fp = $fp;
$this->initialize();
}
function initialize()
{
fwrite($this->fp, self::ESC . "@");
}
function text($str = "")
{
fwrite($this->fp, $str);
}
function feed($lines = 1)
{
if ($lines <= 1)
{
fwrite($this->fp, self::LF);
}
else
{
fwrite($this->fp, self::ESC . "d" . chr($lines));
}
}
/*
* MODE_FONT_A
* MODE_FONT_B
* MODE_EMPHASIZED
* MODE_DOUBLE_HEIGHT
* MODE_DOUBLE_WIDTH
* MODE_UNDERLINE
*/
function select_print_mode($mode)
{
fwrite($this->fp, self::ESC . "!" . chr($mode));
}
function text_normal($str)
{
fwrite($this->fp, self::ESC . "!" . chr(self::MODE_FONT_A));
$this->text($str);
}
function text_small($str)
{
fwrite($this->fp, self::ESC . "!" . chr(self::MODE_FONT_B));
$this->text($str);
}
function text_bold($str)
{
fwrite($this->fp, self::ESC . "!" . chr(self::MODE_EMPHASIZED));
$this->text($str);
}
function text_underline($str)
{
fwrite($this->fp, self::ESC . "!" . chr(self::MODE_UNDERLINE));
$this->text($str);
}
function text_double_height($str)
{
fwrite($this->fp, self::ESC . "!" . chr(self::MODE_DOUBLE_HEIGHT));
$this->text($str);
}
function text_double_width($str)
{
fwrite($this->fp, self::ESC . "!" . chr(self::MODE_DOUBLE_WIDTH));
$this->text($str);
}
// $underline 0 for no underline, 1 for underline, 2 for heavy underline
function set_underline($underline = 1)
{
fwrite($this->fp, self::ESC . "-". chr($underline));
}
// $on true for emphasis, false for no emphasis
function set_emphasis($on = true)
{
fwrite($this->fp, self::ESC . "E". ($on ? chr(1) : chr(0)));
}
// @param boolean $on true for double strike, false for no double strike
function set_double_strike($on)
{
fwrite($this->fp, self::ESC . "G". ($on ? chr(1) : chr(0)));
}
// Font must be FONT_A, FONT_B, or FONT_C.
function set_font($font)
{
fwrite($this->fp, self::ESC . "M" . chr($font));
}
// Justification must be JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT.
function set_justification($justification)
{
fwrite($this->fp, self::ESC . "a" . chr($justification));
}
function justify_left()
{
fwrite($this->fp, self::ESC . "a" . chr(self::JUSTIFY_LEFT));
}
function justify_center()
{
fwrite($this->fp, self::ESC . "a" . chr(self::JUSTIFY_CENTER));
}
function justify_right()
{
fwrite($this->fp, self::ESC . "a" . chr(self::JUSTIFY_RIGHT));
}
// $lines number of lines to feed
function feed_reverse($lines = 1)
{
fwrite($this->fp, self::ESC . "e" . chr($lines));
}
// $mode Cut mode, either CUT_FULL or CUT_PARTIAL
// $lines Number of lines to feed
function cut($mode = self::CUT_FULL, $lines = 3)
{
fwrite($this->fp, self::GS . "V" . chr($mode) . chr($lines));
}
// height of barcode (dots)
function set_barcode_height($height)
{
fwrite($this->fp, self::GS . "h" . chr($height));
}
// string $content
// int $type
function barcode($content, $type = self::BARCODE_CODE39)
{
fwrite($this->fp, self::GS . "k" . chr($type) . $content . self::NUL);
}
/*
* $content : string or URL
* $size : 1 <= n <= 16
* $err : 48 <= n <= 51
*/
function qrcode($content, $size = 5, $err = 50)
{
// data - function 80
fwrite($this->fp, self::GS . "(" . "k" . chr(strlen($content)+3) . chr(0) . chr(49) . chr(80) . chr(48) . $content);
// error correction - function 69
fwrite($this->fp, self::GS . "(" . "k" . chr(3) . chr(0) . chr(49) . chr(69) . chr($err));
// size - function 67
fwrite($this->fp, self::GS . "(" . "k" . chr(3) . chr(0) . chr(49) . chr(67) . chr($size));
// print - function 81
fwrite($this->fp, self::GS . "(" . "k" . chr(3) . chr(0) . chr(49) . chr(81) . chr(48));
}
}