sdf
Reflections & Reference Objects – PrinterJob can’t detect the new default printer in JSP/Servlet
Hi,I solved my problem but I’m not sure it applies to your problems too.
I was using JRPrintServiceExporter and it does not use the default print service, but the first print service returned by PrintServiceLookup.lookupPrintServices(DocFlavor, AttributeSet).
Simply adding this line:
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
And adding that as an export parameter:
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, defaultPrintService);
Solves the problem.
example code is below:
JasperPrint print = JasperFillManager.fillReport(fileName, null, con);
//JasperPrintManager.printReport(print, false);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();//printServiceAttributeSet.add(MediaSizeName.ISO_A4);
//print to your printer by printer name
// printServiceAttributeSet.add(new PrinterName(“Microsoft Office Document Image Writer", null));
printRequestAttributeSet.add(new Copies(iCopies[i]));// print to default printer
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, defaultPrintService);exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
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");
exporter.exportReport();