-
Notifications
You must be signed in to change notification settings - Fork 9
/
printer.class.php
300 lines (290 loc) · 8.37 KB
/
printer.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
class Printer
{
const WIDTH = 380;//打印纸宽度
const HEIGHT = 250;//打印纸高度
const FONT_SIZE = 90;//字体大小(字体高度)
const RATIO = 0.5;//字体宽高比22.5
const OFFSET = 0;//左侧偏移量
const PRINTER_NAME = 'Godex EZ-1105';//打印机名称
const OTHER_FONT_SIZE_RATIO = 0.325;//除姓名外的公司与职位的字体高度比例
public $name = '';
public $number = 1;//签到口编号
public $position = '';
public function __construct($printUserInfo)
{
$key = ['no','name','company'];
foreach ($printUserInfo as $k => $v) {
$this->$key[$k] = iconv("UTF-8","GBK",$v);
}
}
public function start()
{
$handle = printer_open(self::PRINTER_NAME);
printer_abort($handle);
printer_start_doc($handle, "mpd");
printer_start_page($handle);
//设定打印模式
printer_set_option($handle, PRINTER_MODE, "RAW");
//公司第一行位置
// $companyCoordinate = $this->getCompanyCoordinate();
// $companyTextArray = $this->addBlankToTextPrev($this->splitAndCalculateString($this->company));
//$this->printNO($handle);//打印签到口编码
$this->printName($handle);
$this->printCompany($handle);
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);
}
public function startByZPL()
{
$handle = printer_open(self::PRINTER_NAME);
printer_set_option($handle, PRINTER_MODE, "RAW");
$this->printBarCode($handle);
printer_close($handle);
}
public function printBarCode($handle)
{
/**
Q30 字元间距
W 标签宽度
H 设定列印明暗程度
P 列印张数
S 列印速度
AD 列印模式 D:热感模式
C1每张标签复印张数设定
Rx 左边界起点设定
~Q 上边界起印点设定
^O 自动剥纸器/自动贴标机设定 0 关闭
^Dx - 每几张标签裁切一次之设定
^Ex - 停歇点设定
~Rx - 反向列印
^XSET,ROTATION,n - 整页旋转列印
*/
$commands = "
^Q30,3
^W50
^H10
^P1
^S2
^AD
^C1
^R10
~Q+10
^O0
^D0
^E12
~R200
^XSET,ROTATION,0
^L
Dy2-me-dd
Th:m:s
AZ3,86,66,4,4,0,0E,{$this->name}
BA,42,158,2,5,50,0,3,{$this->barcode}
E
";
$success = printer_write($handle, $command);
return true;
}
public function printBarCodeByImg($handle)
{
$generator = new IMGGenerator($this->barcode);
// var_dump($this->barcode);exit;
$bmpPath = $generator->getBmpImagePath();
printer_draw_bmp($handle, $bmpPath, self::OFFSET - 5, 170);
return true;
}
/**
* 打印姓名
* @author wonguohui
* @since 2016-07-04T14:58:22+0800
* @param $handle printer hanle
*/
public function printName($handle)
{
//姓名位置
$nameCoordinate = $this->getNameCoordinate();
$nameTextArray = $this->addBlankToTextPrev($this->splitAndCalculateString($this->name),4);
//姓名字体
$fontForName = $this->getNameFont();
printer_select_font($handle,$fontForName);
printer_draw_text($handle,$nameTextArray['fisrtLine'],self::OFFSET,$nameCoordinate['y']);
//draw line
printer_draw_line($handle,self::OFFSET,$nameCoordinate['y'] + 1.2*self::FONT_SIZE,self::WIDTH + 100,$nameCoordinate['y'] + 1.2*self::FONT_SIZE);
printer_delete_font($fontForName);
return true;
}
/**
* 打印公司信息
* @author wonguohui
* @since 2016-07-04T15:11:41+0800
* @param $handle printer handle
*/
public function printCompany($handle)
{
//公司位置
$companyCoordinate = $this->getCompanyCoordinate();
$companyTextArray = $this->addBlankToTextPrev($this->splitAndCalculateString($this->company));
//公司字体
$fontForCompany = $this->getCompanyFont();
printer_select_font($handle,$fontForCompany);
$lineY = $companyCoordinate['y'];
if(!empty($companyTextArray['secondLine'])){
$fisrtLineY = $companyCoordinate['y'] - 15;
printer_draw_text($handle,$companyTextArray['fisrtLine'],self::OFFSET,$fisrtLineY);
$secondLineY = $fisrtLineY + 3 * self::FONT_SIZE * self::RATIO * self::OTHER_FONT_SIZE_RATIO;
printer_draw_text($handle,$companyTextArray['secondLine'],self::OFFSET,$secondLineY);
$lineY = $secondLineY;
}else{
printer_draw_text($handle,$companyTextArray['fisrtLine'],self::OFFSET,$companyCoordinate['y']);
}
printer_draw_line($handle,self::OFFSET,$lineY + 1.5*self::FONT_SIZE*self::OTHER_FONT_SIZE_RATIO,self::WIDTH + 100,$lineY + 1.5*self::FONT_SIZE*self::OTHER_FONT_SIZE_RATIO);
printer_delete_font($fontForCompany);
return true;
}
public function printNO($handle)
{
$font = $this->getCompanyFont();
printer_select_font($handle,$font);
printer_draw_text($handle, $this->no,350 ,20);
printer_delete_font($font);
return true;
}
/**
* 创建姓名字体
* @author wonguohui
* @since 2016-07-04T11:22:25+0800
*/
public function getNameFont()
{
$fontWidth = self::FONT_SIZE * self::RATIO;
$fontHeight = self::FONT_SIZE;
$fontForName = $this->generateFont($fontHeight,$fontWidth,PRINTER_FW_BOLD);
return $fontForName;
}
/**
* 创建公司字体
* @author wonguohui
* @since 2016-07-04T14:41:56+0800
*/
public function getCompanyFont()
{
$fontWidth = self::FONT_SIZE * self::RATIO * self::OTHER_FONT_SIZE_RATIO;
$fontHeight = self::FONT_SIZE * self::OTHER_FONT_SIZE_RATIO;
$fontForCompany = $this->generateFont($fontHeight,$fontWidth,PRINTER_FW_BOLD);
return $fontForCompany;
}
/**
* 创建字体
* @author wonguohui
* @since 2016-07-04T11:18:49+0800
* @param $fontWidth 字体宽度
* @param $fontHeight 字体高度
* @param $fontWeight 字体粗细
* @return font face
*/
public function generateFont($fontHeight,$fontWidth,$fontWeight=PRINTER_FW_BOLD)
{
return printer_create_font("simhei",$fontHeight,$fontWidth,$fontWeight,false,false,false,0);
}
/**
* 获取姓名的y值坐标,x坐标为添加空格计算所得
* @author wonguohui
* @since 2016-07-04T13:46:00+0800
* @return [x,y]
*/
public function getNameCoordinate()
{
$y = self::HEIGHT * 0.5 - self::FONT_SIZE;
return ['x'=>'','y'=>$y];
}
/**
* 获取公司的y值坐标
* @author wonguohui
* @since 2016-07-04T14:43:31+0800
* @return [x,y]
*/
public function getCompanyCoordinate()
{
$y = self::HEIGHT * 0.5 + 50;
return ['x'=>'','y'=>$y];
}
/**
* 计算中英文字符数,补空格居中显示
* @author wonguohui
* @since 2016-07-04T12:07:44+0800
* @param array $textInfo 字符串信息['中文字符数','英文字符串','第一行文本','第二行文本']
* @param integer $total 默认一行打满文字的字数
*/
public function addBlankToTextPrev(array $textInfo,$total = 14)
{
$key = ['fisrtLine','secondLine'];
foreach ($textInfo as $k => $v) {
if(in_array($k, $key) && !empty($v)){
$lineTextInfo = $this->splitAndCalculateString($v);
$curTextCount = ($total - $lineTextInfo['chinanum'] - 0.5*$lineTextInfo['notchinanum'])/2;
$textInfo[$k] = $this->addBlank($v,$curTextCount);
}
}
return $textInfo;
}
/**
* 根据当前的字符数量(中文加英文)和标签纸一行的字符容量添加空格
* 完成居中
* @author wonguohui
* @since 2016-07-04T11:55:49+0800
* @param $text 字符串
* @param $curTextCount 中文字符加英文字符的总数量
* @param $total 标签纸一行总的字符数
*/
public function addBlank($text,$curTextCount)
{
$blankText = '';
for($i=0;$i<$curTextCount*2;$i++){
$blankText .= iconv("UTF-8","GBK"," ");
}
return $blankText.$text;
}
/**
* 计算并拆分一个字符串
* @author wonguohui
* @since 2016-07-04T12:09:20+0800
* @param $text 字符串文本
* @return ['中文字符数','英文字符串','第一行文本','第二行文本']
*/
public function splitAndCalculateString($text)
{
/**返回数据数组结构 **/
$returnData = [
'chinanum' => 0,
'notchinanum' => 0,
'fisrtLine' => '',
'secondLine' => '',
];
if(empty($text)) return $returnData;
$chinanum = 0;
$notchinanum = 0;
$length = strlen($text);
$fisrtLine = "";
$lastLine = "";
for($i=0;$i<$length;$i++){
if(ord(substr($text,$i,1))<=128){
$notchinanum++;
}else{
$i = $i+1;
$chinanum++;
}
if(($notchinanum + $chinanum) == 11 ){
$fisrtLine = substr($text,0,$i-1);
$lastLine = substr($text,$i-1);
}
}
if(empty($fisrtLine)) $fisrtLine = $text;
$returnData['fisrtLine'] = $fisrtLine;
$returnData['secondLine'] = $lastLine;
$returnData['chinanum'] = $chinanum;
$returnData['notchinanum'] = $notchinanum;
return $returnData;
}
}
?>