2015-06-24 33 views
7

Wszystko czego chcę, to wydrukować JasperReport bez wyboru drukarki. Szukałem go, ale nie ma dobrego rozwiązania, które działa. To jest część zapasowa mojego kodu:Jak wydrukować raporty Jaspera na określonej drukarce?

//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); 

Zamiast prostego printReport chcę wybrać drukarkę. czy jest jakikolwiek sposób to zrobić?

+1

Nie mogę być jedynym, który zmaga się z tym problemem ... –

Odpowiedz

8

Jak to ma być:

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

Oto proste rozwiązanie do drukowania raportu jaspis na konkretnej drukarki Tworzenie Jedna metoda Wybierz drukarki raportu & druku

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

następnie wywołaj tę metodę, np.

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

Chcę drukować na udostępnionej drukarce, Jak rozpoznać nazwę drukarki dla udostępnianych drukarek. Używam nazwy drukarki "POS", wyświetla się jako "POS na serwerze" we współużytkowanej nazwie drukarki. – WARRW

+0

musisz znaleźć nazwę drukarki z ustawień drukarki i użyć jej w kodzie. jakbym użył "Microsoft XPS Document Writer". Aby uniknąć zbędnego kompilowania kodu, można zapisać nazwę drukarki w db i zaktualizować ich nazwę, jeśli zmieni się nazwa drukarki. – CyberAbhay

+0

Tak, problem z tym, że nie działa w przypadku drukarek udostępnionych w sieci. Używam nazwy na ustawieniach drukarki, ale to nie działa dla mnie. – WARRW

2

prostu użyć tego .Nie trzeba więcej kodu obsłużyć Jasper

JasperPrintManager.printReport(jasperPrint, false); 

u używać prawda pokaże okno

Testowałem go Epson TM T82 to działało.

+0

POS jest używany w sklepach itp., Więc w jaki sposób będą wybierać drukarkę za każdym razem w oknie – CyberAbhay

+0

nie trzeba wybierać drukarki podczas działania aplikacji. Masz ustawioną drukarkę Epson jako domyślną drukarkę w komputerze – Zcon

+0

Jeśli mają dwie drukarki zainstalowane na ich osobiste używać i jeden dla klientów (POS)? jeśli mają system rozliczeń hotelowych: jedna drukarka w recepcji i jedna w kuchni. PO PROSTU pomyśl o człowieku z pudełka, jeśli mają dwie drukarki, to co. Wydruki dzienne będą wysyłane do POS, jeśli ustawisz je jako domyślne – CyberAbhay

0

Te kody są przestarzałe. JRPrintServiceExporter.setParameter został przestarzały w JasperReports 5.6. Wprowadzili nowy interfejs eksportera i zmodernizowali wszystkich eksporterów, aby mieć ExporterInput, ReportExportConfiguration, ExporterConfiguration, ExporterOutput. Patrz niżej linkiem

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

Oznacza to, że zamiast setParameter, trzeba stworzyć konfigurację:

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!"); 
}} 

Można drukować przez ten kod na drukarce sieciowej podzielił też.

Powiązane problemy