0

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

4

1 回答 1

2

我尝试使用以下代码,它对我来说工作正常。

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

input type="hidden" name="contextPath" value="<%=request.getContextPath() %>" id="contextPath"/>

第 2 步:在包含的 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');

struts-config.xml 文件的第 3 步

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

* *第 4 步操作(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 Report Print有帮助而不生成报告

于 2013-07-12T15:49:58.420 回答