用Java编写一个打印标签客户端
点击运行启动会打开首页
可以点击预览打印
点击打印可测试成功
打印机结果
前端用的是thymeleaf
代码片段
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="icon" th:href="@{/title.ico}" type="image/x-icon"/>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="col-sm-12 column">
<ol class="breadcrumb">
<li class="active">打印机客户端
</li>
<li class="active">打印机管理
</li>
</ol>
<!-- <a th:href="@{/printService/download}">操作手册.PDF</a> <br/>-->
<div class="table-responsive">
<table class="table table-striped ">
<thead>
<tr>
<th style="text-align:center">打印机名称</th>
<th style="text-align:center">打印机状态</th>
</tr>
</thead>
<tbody>
<!--
这里的 policyPublishDetailDtoList
是 Controller 通过 model.addAttribute("policyPublishDetailDtoList", list);传过来的
th:each th:text都是thymeleaf中常用的标签
-->
<tr th:each="printInfoDto:${printInfoVoDtoList}">
<td th:text="${printInfoDto.printName}" style="text-align:center;vertical-align:middle;"></td>
<td th:text="${printInfoDto.printStatus}" style="text-align:center;vertical-align:middle;"></td>
<td>
<div class="btn-group" th:if="${printInfoDto.status}">
<a class="btn btn-default" th:href="@{/printService/preview(printName=${printInfoDto.printName})}" th:text="${'预览 | ' + printInfoDto.printName}"></a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
后端代码
/**
* 得到全部打印机
*/
@GetMapping("/printList")
public String printList(Model model, HttpServletRequest request) {
PrintInfoVo printInfoVo;
List<PrintInfoVo> printInfoVoList = new ArrayList<>();
List<String> totalPrinterList = PrintUtils.getTotalPrinterList();
if (totalPrinterList.size() > 0) {
//在线打印机
Map<String, String> map = PrintUtils.getZxPrinterMap();
//默认打印机
String defaultPrintName = PrintUtils.getDefaultPrintName();
// 遍历所有打印机的名称
for (String printer : totalPrinterList) {
printInfoVo = new PrintInfoVo();
printInfoVo.setStatus(false);
if (printer.equals(defaultPrintName)) {//默认打印机
printInfoVo.setPrintName(printer);
String PrintStatus = "默认打印机";
if (map.get(printer) == null) {
printInfoVo.setPrintStatus(PrintStatus + "-脱机");
} else {
printInfoVo.setPrintStatus(PrintStatus + "-在线");
printInfoVo.setStatus(true);
}
} else {//其他打印机
printInfoVo.setPrintName(printer);
if (map.get(printer) == null) {
printInfoVo.setPrintStatus("脱机");
} else {
printInfoVo.setPrintStatus("在线");
}
}
printInfoVoList.add(printInfoVo);
}
}
model.addAttribute("printInfoVoDtoList", printInfoVoList);
model.addAttribute("printInfoDto", new PrintInfoVo());
request.getSession().removeAttribute("relativePath");
return "printList";
}
/**
* 打印标签
*/
@GetMapping("/testPrint")
public String testPrint(String printName, Model model, HttpServletRequest request) {
//调用打印逻辑进行打印
PrintServer printServer = new PrintServer();
Result result = printServer.print(PrintUtils.InitialDateString);
model.addAttribute("printName", printName);
model.addAttribute("result", result.getMsg());
request.getSession().removeAttribute("relativePath");
return "result";
}
页面上预览图片和真实打印机打出来的不一致是正常的因为这是测试打印(测试本地电脑是否符合使用打印机客户端调用bartender进行打印)