0%

系统接入TSC条码打印机

最近做历史件档案归档的需求,数据量有几百万,其中一个功能是生成的归档号以条码加姓名的形式打印出来,然后贴到文件袋上。

也没接触过这玩意,只能上网查找资料,最后选定了TSC的TTP 342 pro。

分两种方式实现。

装驱动

img

后续next。。。装下来即可

JAVA实现

项目结构

img

demo

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
package com.tscsdk.mytest;
import com.sun.jna.Library;
import com.sun.jna.Native;

public class PrintFontTest {
public interface TscLibDll extends Library {
TscLibDll INSTANCE = Native.loadLibrary("\\TSCLIB", TscLibDll.class);
int openport(String printerName);
void closeport();
void sendcommand(String command);
void printerfont(String x, String y, String fontType, String rotation, String xMultiplication, String yMultiplication, String content);
int clearbuffer ();
int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
int printlabel (String set, String copy);
int windowsfontUnicodeLengh(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, byte[] content, int length);

}
public static void main(String[] args) {
// 设置打印机名称
String printerName = "TSC TTP-342 Pro";
// 打开打印机端口
int result = TscLibDll.INSTANCE.openport(printerName);
if (result != 1) {
System.out.println("打印机状态不正确....");
return;
}
// 发送打印指令
TscLibDll.INSTANCE.sendcommand("SIZE 60 mm, 40 mm");
TscLibDll.INSTANCE.sendcommand("SPEED 3");
TscLibDll.INSTANCE.sendcommand("DENSITY 12");
TscLibDll.INSTANCE.sendcommand("DIRECTION 1");
TscLibDll.INSTANCE.sendcommand("SET TEAR ON");
TscLibDll.INSTANCE.sendcommand("CODEPAGE UTF-8");

TscLibDll.INSTANCE.clearbuffer();
String preContent = "GD90090716QQQQQQQ";
for (int i = 1;i < 3;i++){
TscLibDll.INSTANCE.clearbuffer();
String gdArchiveNo = preContent + (i+"");
System.out.println("archiveNo::" + gdArchiveNo);
TscLibDll.INSTANCE.windowsfont(60, 200, 60, 0, 0, 0, "Arial", gdArchiveNo);
TscLibDll.INSTANCE.printlabel("1", "1");
}
// 关闭打印机端口
TscLibDll.INSTANCE.closeport();
}
}

jar包:

📎jna-4.5.1.jar

JS实现

装载库

TSCActiveX.dll, TSCLIB.dll 两个dll拷贝到

32bit 系统: C:\Windows\System32

64bit 系统: C:\Windows\SysWOW64

用管理员身份进入cmd,切换到dll安装的位置,输入命令:

1
regsvr32  TSCActiveX.dll

img

demo(拿来即用)

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>打印测试</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>

</head>
<body>
<object id="MYAvtiveX" classid="clsid:0BF5203E-A4C7-4E4F-A637-4B29BAE979FD"> </object>

<button id="bt">打印按钮</button>
<script>

document.getElementById("bt").onclick = function() {
var collection = ["GD2023050511111122", "GD2023050511111133"]; // 要发送的集合
$.ajax({
type: "POST",
url: "http://localhost:8080/tsc/printGdArchiveNo",
data: JSON.stringify(collection),
contentType: "application/json",
success: function(response) {
console.log(response); // 输出后端返回的结果
}
});
};


document.getElementById("bt").onclick = function() {
var TSCObj;
TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");
TSCObj.ActiveXopenport("TSC TTP-342 Pro");
TSCObj.ActiveXsendcommand("SIZE 70 mm, 10 mm");
TSCObj.ActiveXsendcommand("SPEED 2");
TSCObj.ActiveXsendcommand("DENSITY 12");
TSCObj.ActiveXsendcommand("DIRECTION 1");
TSCObj.ActiveXsendcommand("SET TEAR ON");

var map = {
"GD2023072712340007":"东方闪电",
"GD2023072712340008":"得给对的"
};
// 70 * 10
$.each(map,function (key, value) {
console.log(key + ":" + value);
TSCObj.ActiveXclearbuffer();
TSCObj.ActiveXbarcode("80", "6", "128", "80", "2", "0", "3", "2", key);
TSCObj.ActiveXwindowsfont("600", "12", "60", "0", "0", "0", "Arial", value);

TSCObj.ActiveXprintlabel("1","1")
});

TSCObj.ActiveXcloseport()
};
</script>

</body>
</html>

说明

一些方法还是得看去官方查看文档…

成品展示

play