Created
November 18, 2016 07:02
-
-
Save quchie/453ad4c78c63913d32a1953ce46877d1 to your computer and use it in GitHub Desktop.
Jasper Report print directly to physical printer (Jasper report 6.3.x API)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.sf.jasperreports.engine.data.JRXmlDataSource; | |
import net.sf.jasperreports.engine.JRException; | |
import net.sf.jasperreports.engine.JasperCompileManager; | |
import net.sf.jasperreports.engine.JasperExportManager; | |
import net.sf.jasperreports.engine.JasperFillManager; | |
import net.sf.jasperreports.engine.JasperPrint; | |
import net.sf.jasperreports.engine.JasperReport; | |
import net.sf.jasperreports.engine.JasperPrintManager; | |
import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory; | |
import java.awt.print.PrinterJob; | |
import javax.print.attribute.HashPrintRequestAttributeSet | |
import javax.print.attribute.PrintRequestAttributeSet | |
import javax.print.attribute.HashPrintServiceAttributeSet | |
import javax.print.attribute.PrintServiceAttributeSet | |
import javax.print.attribute.standard.PrinterName | |
import javax.print.attribute.standard.MediaSizeName | |
import net.sf.jasperreports.engine.export.JRPrintServiceExporter | |
import net.sf.jasperreports.engine.JRExporterParameter | |
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter | |
import net.sf.jasperreports.export.SimpleExporterInput | |
import net.sf.jasperreports.export.SimplePrintServiceExporterConfiguration | |
import net.sf.jasperreports.engine.JRReport | |
import javax.print.attribute.standard.OrientationRequested | |
//Lets say we get connection from Grails sessionFactory :) | |
def connection = sessionFactory.currentSession.connection() | |
Map reportParameters = new HashMap(); | |
reportParameters.put("parameterName1", "report parameters value 1"); | |
reportParameters.put("parameterName2", "report parameters value 2"); | |
JasperPrint jasperPrint = JasperFillManager.fillReport("REPORT.jasper").getFile().toString(), parameters,connection); | |
//Get the printers job and names | |
PrinterJob printerJob = PrinterJob.getPrinterJob(); | |
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); | |
//Lets set the printer name based on the registered printers driver name | |
String selectedPrinter = "CANON PIXMA 123" | |
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); | |
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 CANON PIXMA, go ahead and print! | |
if(services.length != 0 || services != null){ | |
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(e){ | |
//IF something goes wrong with the printer, lets just print it as PDF? | |
JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\FAILFOLDER\\damnit-fail-to-print.pdf"); | |
e.printStackTrace() | |
return | |
} | |
}else{ | |
//IF you did not set the printer, lets just print it as PDF? | |
JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\FAILFOLDER\\damnit-fail-to-print.pdf"); | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment