Skip to content

Instantly share code, notes, and snippets.

@isu3ru
Created February 26, 2017 19:21
Show Gist options
  • Save isu3ru/95a7ca695c394c7a0f6e62d936289ef2 to your computer and use it in GitHub Desktop.
Save isu3ru/95a7ca695c394c7a0f6e62d936289ef2 to your computer and use it in GitHub Desktop.
A java method to prompt a printer selection and send jasper report print to the selected printer.
private void PrintReportToPrinter(JasperPrint jp) throws Exception {
PrintService[] lps = PrintServiceLookup.lookupPrintServices(null, null);
PrintService srv = (PrintService) JOptionPane.showInputDialog(this, "Select printer", "Printer", JOptionPane.PLAIN_MESSAGE, null, lps, lps[0]);
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(srv);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(new Copies(1));
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.exportReport();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment