引起:java.awt.print.PrinterException:找不到打印服务。在 sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1305)at at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1277)at net.sf.jasperreports.engine.print.JRPrinterAWT.printPages(JRPrinterAWT.java:205)
尝试从 Java 打印 JasperReports 报告时出错 代码
以下是我尝试的代码,它适用于某些操作系统,但在window7中有问题
try {
String report = args[0];
String printText = args[1];
String expiryDate = args[2];
String printerName = args[3];
HashMap hm = new HashMap();
hm.put("printText", printText);
hm.put("expiryDate", expiryDate);
InputStream templateAsStream = ClassLoader.getSystemResourceAsStream(report);
System.out.println(" \n compile report");
JasperReport jasperReport = JasperCompileManager.compileReport(templateAsStream);
// submit parameters
Map parameters = new HashMap();
parameters.put("param1", "jasper report ...");
parameters.put("param2", "...rules");
System.out.println("fill the compiled template");
JasperPrint jp = JasperFillManager.fillReport(jasperReport, hm, new JREmptyDataSource());
System.out.println("\n Page Count" + jp.getPages().size());
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(printerName, null));
printRequestAttributeSet.add(new Copies(1));
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);
System.out.println("export to printer");
JasperPrintManager.printReport(jp, false);
} catch (Exception ex) {
ex.printStackTrace(System.out);
System.out.println("Died");
}