Here is a simple solution to print a jasper report on a specific printer. Create one method to select a printer and print the 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 e.g.
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);
Cyberabbay
source share