【问题标题】:How to print Jasper Reports in a specified printer?如何在指定的打印机上打印 Jasper Reports?
【发布时间】:2015-06-24 14:08:47
【问题描述】:

我想要的只是打印 JasperReport 而无需用户选择打印机。我搜索了它,但没有很好的解决方案。这是我的代码的相关部分:

//compile to .jasper
String report = JasperCompileManager.compileReportToFile(sourceFileName);

//fill the report
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameter, dataSource);

//print the report      
JasperPrintManager.printReport(jasperPrint, true);

我想选择一台打印机,而不是简单的 printReport。有没有办法做到这一点?

【问题讨论】:

  • 我不可能是唯一一个在这个问题上苦苦挣扎的人......

标签: java reporting-services printing jasper-reports


【解决方案1】:

应该是这样的:

try {

    String report = JasperCompileManager.compileReportToFile(sourceFileName);

    JasperPrint jasperPrint = JasperFillManager.fillReport(report, para, ds);

    PrinterJob printerJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
    printerJob.defaultPage(pageFormat);

    int selectedService = 0;

    AttributeSet attributeSet = new HashPrintServiceAttributeSet(new PrinterName(printerNameShort, null));

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

    try {
        printerJob.setPrintService(printService[selectedService]);

    } catch (Exception e) {

        System.out.println(e);
    }
    JRPrintServiceExporter exporter;
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
    printRequestAttributeSet.add(new Copies(1));

    // these are deprecated
    exporter = new JRPrintServiceExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, printService[selectedService]);
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printService[selectedService].getAttributes());
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
    exporter.exportReport();

} catch (JRException e) {
    e.printStackTrace();
}

【讨论】:

    【解决方案2】:

    这是在特定打印机上打印碧玉报告的简单解决方案 为选择打印机和打印报告创建一种方法

    private void PrintReportToPrinter(JasperPrint jp) throws JRException {
        // TODO Auto-generated method stub
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        // printRequestAttributeSet.add(MediaSizeName.ISO_A4); //setting page size
        printRequestAttributeSet.add(new Copies(1));
    
        PrinterName printerName = new PrinterName("Microsoft XPS Document Writer", null); //gets printer 
    
        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(printerName);
    
        JRPrintServiceExporter exporter = new JRPrintServiceExporter();
    
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        exporter.exportReport();
    }
    

    然后像这样调用这个方法

    /* your code*/
    Map parameters = new HashMap();
    parameters.put("ckotid", kid);
    
    try {
        JasperDesign jsd = JRXmlLoader.load("report\\bill\\check_kot.jrxml");
        JasperReport jr = JasperCompileManager.compileReport(jsd);
        JasperPrint jp = JasperFillManager.fillReport(jr, parameters, con);
        //JasperPrintManager.printPage(jp, 0, false);
        //JasperPrint jp =reportEngine.fillReport() ;//it returns stream 
        PrintReportToPrinter(jp);//call method
    

    【讨论】:

    • 我想在共享打印机上打印,如何识别共享打印机的打印机名称。我使用打印机名称“POS”,它在共享打印机名称中显示为“POS on Server”。
    • 您需要从打印机设置中找到打印机名称并在代码中使用它。就像我使用过“Microsoft XPS Document Writer”一样。为避免冗余代码编译,您可以将打印机名称存储在数据库中,并在打印机名称更改时更新它们
    • 是的,问题是它不适用于网络共享打印机。我在打印机设置上有使用名称,但它对我不起作用。
    【解决方案3】:

    这些代码已过时。 JRPrintServiceExporter.setParameter 在 JasperReports 5.6 中被弃用。他们引入了新的接口 Exporter,并对所有导出器进行了改造,使其具有 ExporterInput、ReportExportConfiguration、ExporterConfiguration、ExporterOutput。见以下链接

    http://jasperreports.sourceforge.net/api/net/sf/jasperreports/export/Exporter.html

    这意味着你需要创建配置而不是setParameter:

    private void PrintReportToPrinter(JasperPrint jasperPrint) throws JRException {
    
    //Get the printers names
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    
    //Lets set the printer name based on the registered printers driver name (you can see the printer names in the services variable at debugging) 
    String selectedPrinter = "Microsoft XPS Document Writer";   
    // String selectedPrinter = "\\\\S-BPPRINT\\HP Color LaserJet 4700"; // examlpe to network shared printer
    
    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);
    printRequestAttributeSet.add(new Copies(1));
    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 <selectedPrinter>, go ahead and print!
    if(services != null && services.length != 0){
      for(PrintService service : services){
          String existingPrinter = service.getName();
          if(existingPrinter.equals(selectedPrinter))
          {
              selectedService = service;
              break;
          }
      }
    }
    if(selectedService != null)
    {   
      try{
          //Lets the printer do its magic!
          exporter.exportReport();
      }catch(Exception e){
    System.out.println("JasperReport Error: "+e.getMessage());
      }
    }else{
      System.out.println("JasperReport Error: Printer not found!");
    }}
    

    您也可以通过此代码打印到网络共享打印机。

    【讨论】:

      【解决方案4】:

      只需使用这个。不需要更多的代码来处理 jasper

      JasperPrintManager.printReport(jasperPrint, false);
      

      你使用true它会显示窗口

      我测试过 epson TM T82 它奏效了。

      【讨论】:

      • POS 用于商店等,因此他们将如何每次在窗口中选择打印机
      • 应用程序运行时无需选择打印机。您已将爱普生打印机设置为计算机中的默认打印机
      • 如果他们安装了两台打印机,一台供个人使用,一台供客户 (POS) 使用,那么?如果他们有酒店计费系统:那么接待处一台打印机,厨房一台。如果他们有两台打印机,那就想想开箱即用的人。如果您将其设置为默认值,他们的日常打印件将被发送到 POS
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多