How to print Jasper Reports in a specified printer?

前端 未结 4 1523
慢半拍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 06:02

    Here is a Simple Solution for printing jasper report on specific printer Create One Method for Select printer & print report

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

    then call this method like

    /* 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
    

提交回复
热议问题