Skip to content

Instantly share code, notes, and snippets.

@esaounkine
Created April 29, 2011 07:50
Show Gist options
  • Save esaounkine/948004 to your computer and use it in GitHub Desktop.
Save esaounkine/948004 to your computer and use it in GitHub Desktop.
Silent Print with Jasper class
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
public class SilentPrint {
public void printReport(Vector jasperPrintList, String selectedPrinter) {
try {
//first make sure that requested printService exists
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
PrintService printService = null;
for(PrintService ps : printServices) {
if(ps.getName().equals(selectedPrinter)) {
printService = ps;
break;
}
}
if(printService != null) {
JRExporter jrExporter = new JRPrintServiceExporter();
jrExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,
jasperPrintList); //tell jasper what reports to export
jrExporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE,
printService); //tell jasper to export the report to selected printService
jrExporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
printService.getAttributes());
jrExporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
Boolean.FALSE); //who doesn't like self-explanatory attributes!
jrExporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
Boolean.FALSE);
jrExporter.exportReport();
}
else {
System.err.println("Printer " + selectedPrinter + " not found");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment