java连接打印机并进行打印

首先添加maven依赖

        
            org.apache.pdfbox
            pdfbox
            2.0.24
        

连接同网段的打印机

1.先查询本网段的在线打印机名称

    public JsonResult> getPrinterList() {
        List list = new ArrayList<>();
        HashPrintRequestAttributeSet requestAttributeSet = new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        //查找所有的可用的打印服务
        PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, requestAttributeSet);
        if (printService == null || printService.length == 0) {
            log.info("打印获取失败,未找到可用打印机,请检查。");
        }
        if (printService != null) {
            for (PrintService print : printService) {
                list.add(print.getName());
            }
        }
        return JsonResult.ok(list);
    }

因为打印的时候只需要打印机名称就行了,所以这里只获取了打印机名称。

2.再通过打印机名称来进行打印操作

方法入参为打印机名称和文件。

public Boolean printFile(String printerName, MultipartFile multipartFile) {

}

匹配打印机名称

if (printerName != null) {
                // 查找并设置打印机
                //获得本台电脑连接的所有打印机
                PrintService[] printServices = PrinterJob.lookupPrintServices();
                if(printServices == null || printServices.length == 0) {
                    System.out.print("打印失败,未找到可用打印机,请检查。");
                    return false;
                }
                PrintService printService = null;
                //匹配指定打印机
                for (int i = 0;i < printServices.length; i++) {
                    System.out.println(printServices[i].getName());
                    if (printServices[i].getName().contains(printerName)) {
                        printService = printServices[i];
                        break;
                    }
                }
                if(printService!=null){
                    printJob.setPrintService(printService);
                }else{
                    System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
                    return false;
                }
            }

设置纸张大小、方向、页数等信息

 //设置纸张及缩放
            PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
            //设置多页打印
            Book book = new Book();
            PageFormat pageFormat = new PageFormat();
            //设置打印方向
            pageFormat.setOrientation(PageFormat.PORTRAIT);//纵向
            pageFormat.setPaper(getPaper());//设置纸张
            book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
            printJob.setPageable(book);
            printJob.setCopies(1);//设置打印份数
            //添加打印属性
            HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
            pars.add(Sides.DUPLEX); //设置单双页

最后进行打印

printJob.print(pars);

注意这里需要捕获异常

完整打印代码如下:

public Boolean printFile(String printerName, MultipartFile multipartFile) {
        PDDocument document = null;
        try {
            File file = new File(multipartFile.getOriginalFilename());
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
            document = PDDocument.load(file);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setJobName(file.getName());
            if (printerName != null) {
                // 查找并设置打印机
                //获得本台电脑连接的所有打印机
                PrintService[] printServices = PrinterJob.lookupPrintServices();
                if(printServices == null || printServices.length == 0) {
                    System.out.print("打印失败,未找到可用打印机,请检查。");
                    return false;
                }
                PrintService printService = null;
                //匹配指定打印机
                for (int i = 0;i < printServices.length; i++) {
                    System.out.println(printServices[i].getName());
                    if (printServices[i].getName().contains(printerName)) {
                        printService = printServices[i];
                        break;
                    }
                }
                if(printService!=null){
                    printJob.setPrintService(printService);
                }else{
                    System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
                    return false;
                }
            }
            //设置纸张及缩放
            PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
            //设置多页打印
            Book book = new Book();
            PageFormat pageFormat = new PageFormat();
            //设置打印方向
            pageFormat.setOrientation(PageFormat.PORTRAIT);//纵向
            pageFormat.setPaper(getPaper());//设置纸张
            book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
            printJob.setPageable(book);
            printJob.setCopies(1);//设置打印份数
            //添加打印属性
            HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
            pars.add(Sides.DUPLEX); //设置单双页
            printJob.print(pars);
        } catch (PrinterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }   
            }
        }
        return true;
    }

以上方法自己用于本地启动后连接本地打印机打印,如果服务发布到服务器上,那么打印机和服务所处的网段不一致,就搜索不到打印机,导致无法打印。

解决以上问题可以代码通过socket连接指定ip,在指定ip搜索打印机。

可以通过这个链接查看方法:

硬件系列(一)----------WIFI打印机之使用socket进行打印机搜索_打印机socket协议_freak_csh的博客-CSDN博客

你可能感兴趣的:(java)