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.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(); | |
} | |
} | |
} |
And here’s a usage sample:
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
public void createReport(String[] jasperFileUrls, Map parameters, JRDataSource ds, String type, | |
String selectedPrinter) { | |
Vector jasperPrintList = new Vector(0); | |
for(String s : jasperFileUrls) { | |
try { | |
jasperPrintList.add(JasperFillManager.fillReport((JasperReport)JRLoader.loadObject(s), parameters, ds)); | |
System.out.println("Added report: " + s + " to the printlist"); | |
} | |
catch(JRException jre) { | |
System.err.println("Failed to add the report '" + s + "' due to exception: " + | |
jre.getMessage()); | |
} | |
} | |
try { | |
printReport(jasperPrintList, selectedPrinter); | |
} | |
catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} |
Hope this helps anyone.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete