I'm developing a web application using jsf, and I have create a jasper report file using iReport.
I try to print this file automatically (when I click a button the method send the file directly to the default printer selected in the system).
My problem is that the printing is in the server side (it means that if I tray to print from another user connected to the server,the file is printed in the printer of the server.
My question is: is there a method to print those files in the client printer?
this is my code to print server side but it's not work in the client side, means that it's printed in the server printer,
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PrinterName;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
private void PrintReportToPrinter(JasperPrint jp) throws JRException {
// TODO Auto-generated method stub
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
// printRequestAttributeSet.add(MediaSizeName.ISO_A4); //setting page size
printRequestAttributeSet.add(new Copies(1));
System.out.println("01-*********************");
PrintService[] services = PrinterJob.lookupPrintServices();
for (PrintService prnt : services) {
System.out.println(prnt.getName());
}
System.out.println("02-*********************");
PrinterName printerName = new PrinterName(services[0].getName(), null); //gets printer
System.out.println("03-*********************");
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(printerName);
System.out.println("04-*********************");
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
System.out.println("05-*********************");
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);
exporter.exportReport();
}
public String creerRapportUniqueProjet() {
try {
Map<String, Object> param = new HashMap<>();
param.put("rapportNom", "Projet");
param.put("PAR_IMAGE_BACKGROUND", "/reporting/sources/BackgroundElit.png");
param.put("PAR_IMAGE_HEADER", "/reporting/sources/headerElit.png");
System.out.println("1-*********************");
JRDataSource dataSource = new JRBeanCollectionDataSource(reportingProjetViewFacade.findById(projet));
System.out.println("2-*********************");
JasperPrint print = JasperFillManager.fillReport(getClass().getResourceAsStream("/reporting/sources/projectAllDetail.jasper"), param, dataSource);
System.out.println("3-*********************");
//JasperPrintManager.printPage(jp, 0, false);
//JasperPrint jp =reportEngine.fillReport() ;//it returns stream
PrintReportToPrinter(print);//call method
System.out.println("4-*********************");
} catch (JRException e) {
e.printStackTrace();
System.out.println("Exception = " + e);
}
return "";
}