How to print Jasper Reports in a specified printer?

前端 未结 4 1526
慢半拍i
慢半拍i 2021-01-02 05:22

All I want, is to print a JasperReport without user selecting a printer. I searched for it but there is no good solution that works. This is the relevat part of my code:

4条回答
  •  清歌不尽
    2021-01-02 05:51

    This is how it's supposed to be:

    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();
    }
    

提交回复
热议问题