JasperPrintManager.printReport (print, false) throws me wrong when I raise the WAR

0

Good morning, I'm having a problem with the execution of my project because I want to automatically print a ticket (report) which if I do from an IDE (eclipse) if it works perfect, but when exporting the WAR and pick it up from Tomcat , Glassfish or EAP makes me an exception. The code where the Exception is long is:

JasperReport report = (JasperReport) JRLoader.loadObject(this.getClass().getResourceAsStream("/reportes/Turno.jasper"));

        JasperPrint print = JasperFillManager.fillReport(report,parameters, new JREmptyDataSource());

        JasperExportManager.exportReportToPdfStream(print, out);   

        //// Justo Aqui Larga la Excepcion
        JasperPrintManager.printReport(print, false);

And the long exception is:

net.sf.jasperreports.engine.JRException: Error printing report.
at net.sf.jasperreports.engine.print.JRPrinterAWT.printPages(JRPrinterAWT.java:214)
at net.sf.jasperreports.engine.JasperPrintManager.print(JasperPrintManager.java:242)
at net.sf.jasperreports.engine.JasperPrintManager.print(JasperPrintManager.java:129)
at net.sf.jasperreports.engine.JasperPrintManager.printReport(JasperPrintManager.java:326)
at municipalidad.impresiones.TurnoImpresion.getImprimeTurno(TurnoImpresion.java:51)
at municipalidad.service.TurnoService.getImpresionTurno(TurnoService.java:26)
at municipalidad.rest.TurnoResource.getImprimeTurnoPost(TurnoResource.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:706)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.awt.print.PrinterException: No print service found.
    at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1359)
    at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1331)
    at net.sf.jasperreports.engine.print.JRPrinterAWT
    
asked by NicoGuevaraAtuq 25.01.2018 в 13:14
source

2 answers

0

Your problem lies in the way you call the impression. When you call it the way you do it: JasperPrintManager.printReport(print, false); with the parameter false you are indicating that it does not show the print dialog and will try to print it in the default printer. Maybe you do not have a printer configured by default.

This is what the error tells you at the end:

Caused by: java.awt.print.PrinterException: No print service found.
    
answered by 26.01.2018 в 22:11
0

I already found the solution, I upload the code in case someone else has a similar problem:

JasperReport report = (JasperReport) JRLoader.loadObject(this.getClass().getResourceAsStream("/reportes/Turno.jasper"));

        JasperPrint print = JasperFillManager.fillReport(report,parameters, new JREmptyDataSource());

        JasperExportManager.exportReportToPdfStream(print, out);   

        //// Prueba de impresion
        //JasperPrintManager.printReport(print, false);


        ////// LO NUEVO

        PrinterJob pj = PrinterJob.getPrinterJob();
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

        for (PrintService printService : services) {
            if (printService.getName().equals("NOMBRE DE LA IMPRESORA")){
                pj.setPrintService(printService);
                //JasperPrintManager.printReport(print, false);
            }
        }

        JRPrintServiceExporter exporter;
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
        printRequestAttributeSet.add(new Copies(1));

        // these are deprecated
        exporter = new JRPrintServiceExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, pj.getPrintService());
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, pj.getPrintService().getAttributes());
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        exporter.exportReport();
    
answered by 29.01.2018 в 13:57