Java中,将ZPL字符串直接发送到打印机,为何设置了打印份数多份,但只能打印出来1份

        最近因公司需求,研究将ZPL字符串直接发送到打印机(用的是ZD888T型号的)实现打印标签功能,但发现设置了打印份数,却只能打出1份,有没有大佬知道是为什么?

        代码如下:

 public ResponseEntity aceptParamsToFtlOutPrint(Map params) throws Exception {
        //获取zpl模板填充后的字符串
        String zplData = zplGenerator.processZPLTemplate(params.get("bqfilePath"), params);

        // 查找所有的打印服务
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

        PrintService selectedService = null;

        // 选择打印机
        for (PrintService service : services) {
            if (service.getName().equalsIgnoreCase(params.get("printName"))) {
                selectedService = service;
                break;
            }
        }

        if (selectedService == null) {
            log.error("没有找到指定的打印机:"+params.get("printName"));
            return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).body(Result.fail("没有找到指定的打印机:" + params.get("printName")));
        }

        try {
            // 构建打印请求属性集
            HashPrintRequestAttributeSet printSet = new HashPrintRequestAttributeSet();
            // 打印份数为2份
            printSet.add(new Copies(2));
            // 创建一个DocPrintJob对象
            DocPrintJob job = selectedService.createPrintJob();
            // 创建包含ZPL数据的字节数组
            byte[] data = zplData.getBytes();
            printSet.add(OrientationRequested.PORTRAIT);
            // 创建一个Doc对象,其中包含要打印的数据
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            Doc doc = new SimpleDoc(new ByteArrayInputStream(data), flavor, null);
            // 发送打印作业
            job.print(doc, printSet);

//            PrintUtil.print(zplData,params.get("printName"));
            return new ResponseEntity<>(Result.succ(0,"ZPL数据已发送到打印机"+params.get("printName"),null), HttpStatus.OK);

        } catch (PrintException e) {
            e.printStackTrace();
            log.error("打印失败,"+e.getMessage());
            return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).body(Result.fail("打印失败," + e.getMessage()));
        }
    }

你可能感兴趣的:(java)