-
Notifications
You must be signed in to change notification settings - Fork 861
/
barcode.php
207 lines (202 loc) · 8.53 KB
/
barcode.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
<?php
require __DIR__ . '/../vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
/* Height and width */
$printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
$printer->text("Height and bar width\n");
$printer->selectPrintMode();
$heights = array(1, 2, 4, 8, 16, 32);
$widths = array(1, 2, 3, 4, 5, 6, 7, 8);
$printer -> text("Default look\n");
$printer->barcode("ABC", Printer::BARCODE_CODE39);
foreach($heights as $height) {
$printer -> text("\nHeight $height\n");
$printer->setBarcodeHeight($height);
$printer->barcode("ABC", Printer::BARCODE_CODE39);
}
foreach($widths as $width) {
$printer -> text("\nWidth $width\n");
$printer->setBarcodeWidth($width);
$printer->barcode("ABC", Printer::BARCODE_CODE39);
}
$printer->feed();
// Set to something sensible for the rest of the examples
$printer->setBarcodeHeight(40);
$printer->setBarcodeWidth(2);
/* Text position */
$printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
$printer->text("Text position\n");
$printer->selectPrintMode();
$hri = array (
Printer::BARCODE_TEXT_NONE => "No text",
Printer::BARCODE_TEXT_ABOVE => "Above",
Printer::BARCODE_TEXT_BELOW => "Below",
Printer::BARCODE_TEXT_ABOVE | Printer::BARCODE_TEXT_BELOW => "Both"
);
foreach ($hri as $position => $caption) {
$printer->text($caption . "\n");
$printer->setBarcodeTextPosition($position);
$printer->barcode("012345678901", Printer::BARCODE_JAN13);
$printer->feed();
}
/* Barcode types */
$standards = array (
Printer::BARCODE_UPCA => array (
"title" => "UPC-A",
"caption" => "Fixed-length numeric product barcodes.",
"example" => array (
array (
"caption" => "12 char numeric including (wrong) check digit.",
"content" => "012345678901"
),
array (
"caption" => "Send 11 chars to add check digit automatically.",
"content" => "01234567890"
)
)
),
Printer::BARCODE_UPCE => array (
"title" => "UPC-E",
"caption" => "Fixed-length numeric compact product barcodes.",
"example" => array (
array (
"caption" => "6 char numeric - auto check digit & NSC",
"content" => "123456"
),
array (
"caption" => "7 char numeric - auto check digit",
"content" => "0123456"
),
array (
"caption" => "8 char numeric",
"content" => "01234567"
),
array (
"caption" => "11 char numeric - auto check digit",
"content" => "01234567890"
),
array (
"caption" => "12 char numeric including (wrong) check digit",
"content" => "012345678901"
)
)
),
Printer::BARCODE_JAN13 => array (
"title" => "JAN13/EAN13",
"caption" => "Fixed-length numeric barcodes.",
"example" => array (
array (
"caption" => "12 char numeric - auto check digit",
"content" => "012345678901"
),
array (
"caption" => "13 char numeric including (wrong) check digit",
"content" => "0123456789012"
)
)
),
Printer::BARCODE_JAN8 => array (
"title" => "JAN8/EAN8",
"caption" => "Fixed-length numeric barcodes.",
"example" => array (
array (
"caption" => "7 char numeric - auto check digit",
"content" => "0123456"
),
array (
"caption" => "8 char numeric including (wrong) check digit",
"content" => "01234567"
)
)
),
Printer::BARCODE_CODE39 => array (
"title" => "Code39",
"caption" => "Variable length alphanumeric w/ some special chars.",
"example" => array (
array (
"caption" => "Text, numbers, spaces",
"content" => "ABC 012"
),
array (
"caption" => "Special characters",
"content" => "$%+-./"
),
array (
"caption" => "Extra char (*) Used as start/stop",
"content" => "*TEXT*"
)
)
),
Printer::BARCODE_ITF => array (
"title" => "ITF",
"caption" => "Variable length numeric w/even number of digits,\nas they are encoded in pairs.",
"example" => array (
array (
"caption" => "Numeric- even number of digits",
"content" => "0123456789"
)
)
),
Printer::BARCODE_CODABAR => array (
"title" => "Codabar",
"caption" => "Varaible length numeric with some allowable\nextra characters. ABCD/abcd must be used as\nstart/stop characters (one at the start, one\nat the end) to distinguish between barcode\napplications.",
"example" => array (
array (
"caption" => "Numeric w/ A A start/stop. ",
"content" => "A012345A"
),
array (
"caption" => "Extra allowable characters",
"content" => "A012$+-./:A"
)
)
),
Printer::BARCODE_CODE93 => array (
"title" => "Code93",
"caption" => "Variable length- any ASCII is available",
"example" => array (
array (
"caption" => "Text",
"content" => "012abcd"
)
)
),
Printer::BARCODE_CODE128 => array (
"title" => "Code128",
"caption" => "Variable length- any ASCII is available",
"example" => array (
array (
"caption" => "Code set A uppercase & symbols",
"content" => "{A" . "012ABCD"
),
array (
"caption" => "Code set B general text",
"content" => "{B" . "012ABCDabcd"
),
array (
"caption" => "Code set C compact numbers\n Sending chr(21) chr(32) chr(43)",
"content" => "{C" . chr(21) . chr(32) . chr(43)
)
)
)
);
$printer->setBarcodeTextPosition(Printer::BARCODE_TEXT_BELOW);
foreach ($standards as $type => $standard) {
$printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
$printer->text($standard ["title"] . "\n");
$printer->selectPrintMode();
$printer->text($standard ["caption"] . "\n\n");
foreach ($standard ["example"] as $id => $barcode) {
$printer->setEmphasis(true);
$printer->text($barcode ["caption"] . "\n");
$printer->setEmphasis(false);
$printer->text("Content: " . $barcode ["content"] . "\n");
$printer->barcode($barcode ["content"], $type);
$printer->feed();
}
}
$printer->cut();
$printer->close();