如何在jsp页面中预览报表而不在服务器上保存html或pdf文件?

时间:2012-12-25 04:44:36

标签: jasper-reports report liferay portlet liferay-6

以下是我在portletAction中的serveResource方法中调用的函数

public String ajax_GenerateReportsforCampaignAdvertiseDateRangeWithCustomQuery(
            ResourceRequest request, ResourceResponse response,
            String str_camp_id) throws Exception {


        PortletContext pc = request.getPortletSession().getPortletContext();
        String Pathjrxml = pc
                .getRealPath("/src_reports/Campaign_Advertise_DateRange_Report.jrxml");
        String pathjasper = pc
                .getRealPath("/src_reports/Campaign_Advertise_DateRange_Report.jasper");

        _report_path = pc.getRealPath("htmlreport/report");
        _report_name = Calendar.getInstance().getTimeInMillis() + "";
        _report_path = _report_path + _report_name;

        String str_start_date = request.getParameter("f_start_date");
        String str_end_date = request.getParameter("f_end_date");
        Date filter_start_date = null;
        Date filter_end_date = null;

        List<CampaignReport> temp_campReportList = new ArrayList<CampaignReport>(
                8);

        log.info("ajax_Generate Reports For Campaign Advertise With Date Range Enter");

        /** Dateformat which will convert filter date string in to specified **/

        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

        filter_start_date = dateFormat.parse(str_start_date);

        filter_end_date = dateFormat.parse(str_end_date);

        str_end_date = dateFormat.format(filter_end_date);

        log.info("Filter Start Date : " + filter_start_date);
        log.info("Filter End Date : " + filter_end_date);

        ReportCustomQuery cust = new ReportCustomQuery();
        String pathImage = request.getContextPath()
                + "/eMenuAdvertise-portlet/img/eMenu Logo_1.png";
        temp_campReportList = cust.GetAdvertiseListByCampaignsForReports(
                Long.parseLong(str_camp_id), filter_start_date,
                filter_end_date, pathImage);



        System.setProperty("java.awt.headless", "true");

        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(
                temp_campReportList);
        System.out.println(" bean collection "
                + beanCollectionDataSource.getRecordCount());
        System.out.println("Compile Start");





        JasperCompileManager.compileReportToFile(Pathjrxml, pathjasper);
        JasperPrint jasperPrint = JasperFillManager.fillReport(pathjasper,
                new HashMap<String, Object>(), beanCollectionDataSource);

        log.info("Compile End");
        log.info(" report path " + _report_path);


        JasperExportManager.exportReportToHtmlFile(jasperPrint, _report_path
                + ".html");
        JasperExportManager.exportReportToPdfFile(jasperPrint, _report_path
                + ".pdf");

        log.info("ajax_GenerateReports Leave");
        return _report_name;
    }

以及调用此方法的ajax调用正在跟随

<script type="text/javascript">
    function GenerateReport(addToDo) {
        var camp_ID = document.getElementById('camp_id').value;
        var f_start_date = document
                .getElementById('Start_validBeforeDatepicker').value;
        var f_end_date = document.getElementById('End_validAfterDatepicker').value;
        $.ajax({
            url : addToDo,
            data : {
                "camp_id" : camp_ID,
                "f_start_date" : f_start_date,
                "f_end_date" : f_end_date,
                "CMD" : camp_ID
            },
            type : "GET",
            timeout : 20000,
            dataType : "text",
            async : false,
            success : function(data) {

                createRenderURL(data);

            }
        });

以下是我的creatRenderUrl javascript,它为新创建的html文件创建动态资源库,然后我在iframe中显示html页面作为预览

function createRenderURL(str_path,contextpath,resourceurl) {



    AUI().use('liferay-portlet-url', function(A) {

        var renderURL = Liferay.PortletURL.createRenderURL();
        renderURL.setParameter("jspPage", "/htmlreport/report" + str_path);
        renderURL.setPortletId("eMenuAdvertise_WAR_eMenuAdvertiseportlet");


        var p= "<%=renderRequest.getContextPath()%>";
        var downloadurl="/eMenuAdvertise-portlet/htmlreport/report"+ str_path+".pdf";

        var x=document.getElementById("outPopUp");
        x.style.visibility="hidden";

        document.getElementById('reportpreview').src = "/eMenuAdvertise-portlet/htmlreport/report"+ str_path+".html";


        $("#download").click(function () {

            DownloadFile(downloadurl,resourceurl);


});

         return downloadurl;

    });

}

但我想要做的是预览报告而不保存任何文件?在飞行中?

1 个答案:

答案 0 :(得分:2)

编译jrxml后,调用JasperRunManager.runReportToPdf而不是JasperExportManager,它将返回报告的字节数组。您可以将此字节数组返回给客户端,而无需将报告保存在文件中。

这样的事情:

JasperCompileManager.compileReportToFile(Pathjrxml, pathjasper);
byte[] report = JasperRunManager.runReportToPdf(pathjasper, new HashMap<String, Object>(), beanCollectionDataSource);