PrintServices是否能够找到打印机并在客户端打印机中打印

时间:2018-12-09 13:24:17

标签: java

我需要直接将碧玉打印到客户端。我的代码在Windows Server中。当我尝试打印时,PrintServices []在服务器(而不是客户端计算机)中获取打印机。在本地系统上工作正常

我的代码是

PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

        //Lets set the printer name based on the registered printers driver name
        String selectedPrinter = settingsVO.getPrinterName();
        System.out.println("Number of print services: " + services.length);
        PrintService selectedService = null;
        //Set the printing settings
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(MediaSizeName.ISO_A4);
        if (jasperPrint.getOrientationValue() == net.sf.jasperreports.engine.type.OrientationEnum.LANDSCAPE) { 
          printRequestAttributeSet.add(OrientationRequested.LANDSCAPE); 
        } else { 
          printRequestAttributeSet.add(OrientationRequested.PORTRAIT); 
        } 
        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(new PrinterName(selectedPrinter, null));
        JRPrintServiceExporter exporter = new JRPrintServiceExporter();
        SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration();
        configuration.setPrintRequestAttributeSet(printRequestAttributeSet);
        configuration.setPrintServiceAttributeSet(printServiceAttributeSet);
        configuration.setDisplayPageDialog(false);
        configuration.setDisplayPrintDialog(false);

        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setConfiguration(configuration);
        //Iterate through available printer, and once matched with our CANON PIXMA, go ahead and print!
        if(services.length != 0 || services != null){
          for(PrintService service : services){
              String existingPrinter = service.getName();
              System.out.println("service"+service.getName());
              if(existingPrinter.equals(selectedPrinter))
              {
                  selectedService = service;
                  break;
              }
          }
        }
        if(selectedService != null)
        {   
          try{
              //Lets the printer do its magic!
              exporter.exportReport();
          }catch(Exception e){
            //IF something goes wrong with the printer, lets just print it as PDF?
              System.out.println("PRINTER SET NOT PRINTING");
            JasperExportManager.exportReportToPdfFile(jasperPrint, "D:\\Invoice.pdf");
            e.printStackTrace();

          }
        }else{
          //IF you did not set the printer, lets just print it as PDF?
            System.out.println("NOT PRINTER SET");
          JasperExportManager.exportReportToPdfFile(jasperPrint, "D:\\Invoice.pdf");
        }

Pl。指导我我需要改变的地方

在本文中,[[Print JasperReports client-side?)浏览器将提示用户将报告另存为PDF文件。用户可以打印PDF。

但是我需要直接打印到客户端打印机,而无需在浏览器中另存为pdf,然后再打印

0 个答案:

没有答案