导出托盘标签图片ZIP
This commit is contained in:
parent
25a88cbeb7
commit
ac8e64884d
|
|
@ -700,43 +700,36 @@ public class StructuralPackageOrderController extends BaseController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 导出托盘标签图片(多个托盘合并为一张长图)
|
* 导出托盘标签图片ZIP(多个托盘分别生成图片并打包成ZIP)
|
||||||
* @param id 订单id
|
* @param id 订单id
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "exportTrayImage", produces = MediaType.IMAGE_PNG_VALUE)
|
@PostMapping(value = "exportTrayImageZip", produces = "application/zip")
|
||||||
public ResponseEntity<byte[]> exportTrayImage(@Valid @RequestParam @NotNull Long id) throws Exception {
|
public ResponseEntity<byte[]> exportTrayImageZip(@Valid @RequestParam @NotNull Long id) throws Exception {
|
||||||
// 根据订单ID获取所有托盘
|
// 根据订单ID获取所有托盘
|
||||||
List<WmsStructuralPackageOrderTray> trays = structuralPackageOrderTrayService.getList(id);
|
List<WmsStructuralPackageOrderTray> trays = structuralPackageOrderTrayService.getList(id);
|
||||||
VUtil.trueThrowBusinessError(CollectionUtil.isEmpty(trays)).throwMessage("数据不存在");
|
VUtil.trueThrowBusinessError(CollectionUtil.isEmpty(trays)).throwMessage("数据不存在");
|
||||||
|
|
||||||
// 为每个托盘生成HTML内容,并拼接成一个完整的HTML
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
StringBuilder mergedHtml = new StringBuilder();
|
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||||
mergedHtml.append("<!DOCTYPE html><html><head><meta charset='UTF-8'/></head><body style='margin:0;padding:0;'>");
|
for (WmsStructuralPackageOrderTray tray : trays) {
|
||||||
|
DeliverStructuralPackageOrderExtendVO trayVO = structuralPackageOrderTrayService.getInfo(tray.getId());
|
||||||
for (WmsStructuralPackageOrderTray tray : trays) {
|
VUtil.trueThrowBusinessError(Objects.isNull(trayVO)).throwMessage("数据不存在");
|
||||||
DeliverStructuralPackageOrderExtendVO trayVO = structuralPackageOrderTrayService.getInfo(tray.getId());
|
trayVO.setQrCode(QRCodeUtil.generateQRCodeBase64(trayVO.getTrayNo(), 200, 200));
|
||||||
VUtil.trueThrowBusinessError(Objects.isNull(trayVO)).throwMessage("数据不存在");
|
Map<String, Object> variables = new HashMap<>();
|
||||||
trayVO.setQrCode(QRCodeUtil.generateQRCodeBase64(trayVO.getTrayNo(), 200, 200));
|
variables.put("info", trayVO);
|
||||||
Map<String, Object> variables = new HashMap<>();
|
String html = ThymeleafUtil.generator("/template/qrcode/", "tray-label", ".html", variables);
|
||||||
variables.put("info", trayVO);
|
ZipEntry entry = new ZipEntry(trayVO.getTrayNo() + ".png");
|
||||||
String html = ThymeleafUtil.generator("/template/qrcode/", "tray-label", ".html", variables);
|
zos.putNextEntry(entry);
|
||||||
// 提取body内容并追加
|
byte[] imageBytes = HtmlToImageUtil.convertToPng(html, 600);
|
||||||
int bodyStart = html.indexOf("<body");
|
zos.write(imageBytes, 0, imageBytes.length);
|
||||||
int bodyEndStart = html.indexOf(">", bodyStart) + 1;
|
zos.closeEntry();
|
||||||
int bodyEnd = html.lastIndexOf("</body>");
|
|
||||||
if (bodyStart != -1 && bodyEnd != -1) {
|
|
||||||
mergedHtml.append(html.substring(bodyEndStart, bodyEnd));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
byte[] zipBytes = baos.toByteArray();
|
||||||
mergedHtml.append("</body></html>");
|
|
||||||
|
|
||||||
// 将合并后的HTML转换为图片
|
|
||||||
byte[] imageBytes = HtmlToImageUtil.convertToPng(mergedHtml.toString(), 600);
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setContentType(MediaType.IMAGE_PNG);
|
headers.setContentType(MediaType.valueOf("application/zip"));
|
||||||
headers.setContentLength(imageBytes.length);
|
headers.setContentLength(zipBytes.length);
|
||||||
return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
|
return new ResponseEntity<>(zipBytes, headers, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue