2015-06-24 15 views
7

Tek istediğim, bir yazıcı seçmeden kullanıcı bir JasperReport yazdırmaktır. Onu aradım ama işe yarayan iyi bir çözüm yok. ben bir yazıcıyı seçmek istediğinizJasper Raporları belirli bir yazıcıda nasıl yazdırılır?

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

yerine basit printReport: Bu relevat benim kod parçasıdır. Bunu yapmanın bir yolu var mı?

+1

Bu sorunla uğraşan tek kişi olamam ... –

cevap

8

Bu da olması gerekiyordu nasıl: Burada

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

Yazıcı seç & baskı raporuna

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

için bir Yöntem oluşturma belirli bir yazıcıda Jasper raporu yazdırmak için Basit Çözüm ise sonra bu yöntemi

/* 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 
olarak adlandırın.
+0

Paylaşılan yazıcıda yazdırmak istiyorum, Paylaşılan yazıcılar için yazıcı adını tanıma. Yazıcı adı "POS" kullanıyorum, Paylaşılan yazıcı adında "Sunucuda POS" olarak görünüyor. – WARRW

+0

Yazıcı ayarlarından yazıcı adını bulmanız ve bunu kod içinde kullanmanız gerekir. "Microsoft XPS Belge Yazıcısı" kullandığım gibi. Yedek kod derlemesinden kaçınmak için yazıcı adını db'ye kaydedebilir ve yazıcı adı – CyberAbhay

+0

Evet ise Sorunlarını güncelleyebilir, Sorun ağ paylaşımlı yazıcılarda çalışmaz. Yazıcı ayarlarında isim kullanıyorum ama benim için çalışmıyor. – WARRW

2

Sadece bunu kullanın o u test ettim İşe yaradı epson TM T82 pencere

gösterecektir gerçek kullanmak Jasper

JasperPrintManager.printReport(jasperPrint, false); 

işlemek daha koduna gerek yer.Hiçbir.

+0

POS, mağazalarda vs. kullanılır, böylece her seferinde yazıcıyı her seferinde nasıl seçecekleri – CyberAbhay

+0

no'lu yazıcıyı uygulama çalışırken kullanmaya gerek kalmaz. Epson yazıcıyı varsayılan yazıcı olarak – Zcon

+0

olarak ayarladıysanız o zamanlar ve müşteriler için (POS) kullanın? Otel fatura sistemi varsa: resepsiyonda bir yazıcı ve bir mutfak sonra. SADECE iki yazıcı varsa kutunun adam düşünmek o zaman varsayılan olarak ayarlarsanız günlük baskı POS'a gönderilecektir. – CyberAbhay

0

Bu kodlar geçerliliğini yitirdi. JRPrintServiceExporter.setParameter, JasperReports 5.6'da kullanımdan kaldırıldı. Onlar ExporterInput, ReportExportConfiguration, ExporterConfiguration, ExporterOutput sahip olmak için yeni ihracatçı İhracatçı ve tüm ihracatçıları uyarladılar.

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

Sen de bir ağ paylaşılan yazıcıya bu kodla yazdırabilirsiniz:

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

linke

aşağıya bakın Bu yerine setParameter, sen yapılandırma oluşturmak gerektiği anlamına gelir.

İlgili konular