2015-06-24 100 views
7

所有我想要的,是打印的JasperReport無需用戶選擇打印機。我搜索了它,但沒有好的解決方案。這是我的代碼的relevat部分:如何在指定的打印機中打印Jasper Reports?

//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我想選擇一臺打印機。有沒有辦法做到這一點?

+1

我不能是唯一一個與此問題所困擾...... –

回答

8

這是它是如何應該是:

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

下面是具體的打印機進行打印碧玉報告一個簡單的解決方案 創建選擇打印機&打印報告

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

我想打印共享的打印機上,如何共享打印機識別打印機名稱。我使用打印機名稱「POS」,它在共享打印機名稱中顯示爲「POS服務器」。 – WARRW

+0

您需要從打印機設置中找到打印機名稱並在代碼中使用該名稱。就像我使用過「Microsoft XPS Document Writer」一樣。爲避免冗餘代碼編譯,您可以將打印機名稱存儲在數據庫中,並更新其打印機名稱更改 – CyberAbhay

+0

是的,問題是它不適用於網絡共享打印機。我在打印機設置上使用了名稱,但它不適用於我。 – WARRW

2

只是用這個。沒有需要更多的代碼是處理碧玉

JasperPrintManager.printReport(jasperPrint, false); 

u使用真實的,它會顯示窗口

我測試了愛普生TM T82 它的工作。

+0

POS在店等使用,因此他們將如何選擇打印機的每次窗口 – CyberAbhay

+0

沒有必要選擇打印機時應用running.you已設置爲愛普生打印機在計算機 – Zcon

+0

默認打印機如果他們有兩臺打印機安裝了一個用於其個人然後使用和一個客戶(POS)?如果他們有酒店收費系統:那麼接待處有一臺打印機,然後是廚房。如果他們有兩臺打印機,那麼他們應該認真思考一下,如果您將其設置爲默認打印機,那麼他們的日常打印將被髮送到POS – CyberAbhay

0

這些代碼已經過時。在JasperReports 5.6中已棄用JRPrintServiceExporter.setParameter。他們引進了新的界面出口商,並對所有出口商進行了改造,使其具有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!"); 
}} 

您可以通過這個代碼打印到網絡共享打印機了。