-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathphp-print.php
250 lines (238 loc) · 6.32 KB
/
php-print.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
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* Copyright (c) 2014 Michael Billington <[email protected]>,
* with additions by Warren Doyle (wdoyle)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* If you wish to expand this library you can use the below link:
* http://content.epson.de/fileadmin/content/files/RSD/downloads/escpos.pdf
*/
define("DEBUG_MODE", 0);
if(DEBUG_MODE == 1)
{
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
class phpprint {
/* ASCII codes */
const NUL = "\x00";
const LF = "\x0a";
const ESC = "\x1b";
const GS = "\x1d";
/* Print mode constants */
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;
/* Fonts */
const FONT_A = 0;
const FONT_B = 1;
const FONT_C = 2;
/* Justifications */
const JUSTIFY_LEFT = 0;
const JUSTIFY_CENTER = 1;
const JUSTIFY_RIGHT = 2;
/* Cut types */
const CUT_FULL = 65;
const CUT_PARTIAL = 66;
/* Barcode types */
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($head = null) {
//THIS IS THE SERIAL PORT YOU ARE OPENING
$fp = fopen($head,"w");
$this -> fp = $fp;
$this -> initialize();
}
/**
* Add text to the buffer
*
* @param string $str Text to print
*/
function text($str = "") {
if(DEBUG_MODE == 1)
echo $str;
fwrite($this -> fp, $str);
}
/**
* Print and feed line / Print and feed n lines
*
* @param int $lines Number of lines to feed
*/
function feed($lines = 1) {
if($lines <= 1) {
fwrite($this -> fp, self::LF);
} else {
fwrite($this -> fp, self::ESC . "d" . chr($lines));
}
}
/**
* Select print mode(s).
*
* Arguments should be OR'd together MODE_* constants:
* MODE_FONT_A
* MODE_FONT_B
* MODE_EMPHASIZED
* MODE_DOUBLE_HEIGHT
* MODE_DOUBLE_WIDTH
* MODE_UNDERLINE
*
* @param int $mode
*/
function select_print_mode($mode = self::NUL) {
fwrite($this -> fp, self::ESC . "!" . chr($mode));
}
function reverse_mode($rev = 0) {
fwrite($this -> fp, self::GS . "B" . chr($rev));
}
/**
* Turn underline mode on/off
*
* @param int $underline 0 for no underline, 1 for underline, 2 for heavy underline
*/
function set_underline($underline = 1) {
fwrite($this -> fp, self::ESC . "-". chr($underline));
}
/**
* Initialize printer
*/
function initialize() {
fwrite($this -> fp, self::ESC . "@");
}
/**
* Turn emphasized mode on/off
*
* @param boolean $on true for emphasis, false for no emphasis
*/
function set_emphasis($on = false) {
fwrite($this -> fp, self::ESC . "E". ($on ? chr(1) : chr(0)));
}
/**
* Turn double-strike mode on/off
*
* @param boolean $on true for double strike, false for no double strike
*/
function set_double_strike($on = false) {
fwrite($this -> fp, self::ESC . "G". ($on ? chr(1) : chr(0)));
}
/**
* Select character font.
* Font must be FONT_A, FONT_B, or FONT_C.
*
* @param int $font
*/
function set_font($font = self::FONT_A) {
fwrite($this -> fp, self::ESC . "M" . chr($font));
}
/**
* Select justification
* Justification must be JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT.
*/
function set_justification($justification = self::JUSTIFY_LEFT) {
fwrite($this -> fp, self::ESC . "a" . chr($justification));
}
/**
* Print and reverse feed n lines
*
* @param int $lines number of lines to feed
*/
function feed_reverse($lines = 1) {
fwrite($this -> fp, self::ESC . "e" . chr($lines));
}
/**
* Cut the paper
*
* @param int $mode Cut mode, either CUT_FULL or CUT_PARTIAL
* @param int $lines Number of lines to feed
*/
function cut($mode = self::CUT_FULL, $lines = 3) {
fwrite($this -> fp, self::GS . "V" . chr($mode) . chr($lines));
}
/**
* Set barcode height
*
* @param int $height Height in dots
*/
function set_barcode_height($height = 8) {
fwrite($this -> fp, self::GS . "h" . chr($height));
}
/**
* Print a barcode
*
* @param string $content
* @param int $type
*/
function barcode($content, $type = self::BARCODE_CODE39) {
fwrite($this -> fp, self::GS . "k" . chr($type) . $content . self::NUL);
}
/**
* This will generate a Barcode and print it directly to the Printer
*
* @param string $content
* @param int $type
* @param int $height
*
*/
function generateBarcode($content, $type, $height)
{
$this->set_barcode_height($height);
$this->barcode($content, $type);
$this->feed();
}
/*
*
* This will increase the font used on the page whilst it is true
* @param bool $on
*
*/
function enlargePrint($on = false)
{
if($on == true)
$this->select_print_mode(self::MODE_DOUBLE_HEIGHT);
else
$this->select_print_mode();
}
/*
* This will print an empty line
*/
function newline()
{
$this->text("\n");
}
/**
* Generate a pulse, for opening a cash drawer if one is connected.
* The default settings should open an Epson drawer.
*
* @param int $pin 0 or 1, for pin 2 or pin 5 kick-out connector respectively.
* @param int $on_ms pulse ON time, in milliseconds.
* @param int $off_ms pulse OFF time, in milliseconds.
*/
function pulse($pin = 0, $on_ms = 120, $off_ms = 240) {
fwrite($this -> fp, self::ESC . "p" . chr($m + 48) . chr($t1 / 2) . chr($t2 / 2));
}
}