printReport打印jasper报告时出错

printReport打印jasper报告时出错

本文介绍了使用JasperPrintManager.printReport打印jasper报告时出错(print,true);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码: -

This below is my code:-

Map parameters = new HashMap();
parameters.put("printer", "1010111");
FileInputStream file = new FileInputStream(new File(getServletContext().getRealPath("/Reports/report1.jasper")));
JasperPrint print = JasperFillManager.fillReport(file, parameters, new JRBeanCollectionDataSource(reports));
JasperPrintManager.printReport(print, true);

我正在尝试打印jasper页面。使用JasperPrintManager.printReport(print,false)时没有任何问题但是当我使用true时,我收到以下错误。

I am trying to print the jasper page. I don't have any trouble when using "JasperPrintManager.printReport(print, false)" but when i use "true", I get the following error.

Severe:   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 Reports.Closing_Report_Report.doGet(Closing_Report_Report.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.awt.HeadlessException
    at sun.awt.windows.WPrinterJob.printDialog(WPrinterJob.java:576)
    at net.sf.jasperreports.engine.print.JRPrinterAWT.printPages(JRPrinterAWT.java:198)
    ... 33 more


推荐答案

重要的是要理解的是,它不是用

What is important to understand is that it is not the browser that is printing with

JasperPrintManager.printReport(print, false)

它实际上是打印到您首选打印机的服务器,因此Web用户将始终在服务器打印机上打印(而不是在他自己的打印机上)。

Its actually the server that is printing to your preferred printer, hence a web user will always print on the server printer (not on his own printer).

这就是为什么

JasperPrintManager.printReport(print, true)

不起作用,你不能告诉服务器打开 PrintDialog 。此调用用于桌面应用程序。

does not work, you can't tell the server to open the PrintDialog. This call is used in desktop application.

直接在客户端打印机上打印是不可能的(不包括需要在客户端浏览器上安装的浏览器插件的开发)。如果这可能没有特殊的插件,我们的打印机将整天打印垃圾邮件。

It's impossibile to print directly on client printer (excluding the development of browser plugin that needs to be installed on client's browser). If this was possibile with out special plugin's our printers would be printing spam all day.

您可以获得的关闭是将报告导出到浏览器然后自动打开打印对话框。

The closes you can get is to export the report to the browser and then automatically open the print dialog.

这是导出为pdf的示例:

This is an example exporting to pdf: Automatically open the printer dialog after providing PDF download

这篇关于使用JasperPrintManager.printReport打印jasper报告时出错(print,true);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:11