Jasper报告在客户端打印而不生成报告

时间:2013-07-12 05:05:03

标签: java

我在我的项目中使用struts和jasper报告,现在我遇到问题,每当我尝试打印报告时,打印机对话框出现在服务器机器上,我希望它在客户端而不导出任何格式的报告

1 个答案:

答案 0 :(得分:2)

我尝试了以下代码镫骨,它对我来说很好。

第1步: 在基础Jsp(report.jsp)

input type =“hidden”name =“contextPath”value =“<%= request.getContextPath()%>” ID = “contextPath中”/>

第2步: 在includesed js(report.js)

var contextPath=document.getElementById("contextPath").value;
var url=contextPath+"/generateRecptReport.do?method=generateRecptReport;
popupWin= window.open(url,'Report','menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=1, height=1 left=0, top=0');

第3步 在struts-config.xml文件

<action path="/generateRecptReport" scope="request" parameter="method" type="com.cm.actions.ReceiptMakerProcessAction">
</action>

* *第4步 on Action(Java代码)(ReceiptMakerProcessAction.java)

public ActionForward generateRecptReport(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
{
logger.info("In generateRecptReport");          
        String p_company_logo=getServlet().getServletContext().getRealPath("/")+"reports/logo.bmp";
        String reportPath="/reports/MSSQLREPORTS/";
        String reportName="receiptMemoReport";          
        ReportsDAO reportdao = (ReportsDAO)DaoImplInstanceFactory.getDaoImplInstance(ReportsDAO.IDENTITY);
        logger.info("Implementation class: "+reportdao.getClass());
        reportName=reportdao.getReceiptReportName();
        if(CommonFunction.checkNull(reportName).trim().equalsIgnoreCase(""))
            reportName="receiptMemoReport";             

        Connection connectDatabase = ConnectionDAO.getConnection();     
        Map<Object,Object> hashMap = new HashMap<Object,Object>();
        hashMap.put("p_company_logo",p_company_logo);
        hashMap.put("IS_IGNORE_PAGINATION",true);
        try
        {
            InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream(reportPath+reportName+".jasper");
            JasperPrint jasperPrint = null; 
            PrintWriter out=response.getWriter();
            out.append("<head><script type='text/javascript' src='"+request.getContextPath()+"/js/report/report.js'></script></head>");
            out.append("<body onLoad='self.print();self.close()'></body>");     
            response.setContentType("text/html");       
            String htmlReportFileName=reportName+".html";
            JRHtmlExporter exporter = new JRHtmlExporter();     
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);     
            exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, out);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN ,Boolean.FALSE);
            exporter.setParameter(JRHtmlExporterParameter.IGNORE_PAGE_MARGINS ,Boolean.TRUE); 
            exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
            exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
            exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
            exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML,"");
            exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());
            float f1=1.2f;  
            exporter.setParameter(JRHtmlExporterParameter.ZOOM_RATIO ,f1);
            exporter.exportReport();                    
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally 
        {
            ConnectionDAO.closeConnection(connectDatabase, null);
            hashMap.clear();
        }
        return null;
    }

希望在没有生成报告的情况下对客户端的Jasper报告打印有帮助