1

以下是我在 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 文件创建一个动态 resourceurl,然后我在 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;

    });

}

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

4

1 回答 1

2

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

像这样的东西:

JasperCompileManager.compileReportToFile(Pathjrxml, pathjasper);
byte[] report = JasperRunManager.runReportToPdf(pathjasper, new HashMap<String, Object>(), beanCollectionDataSource);
于 2012-12-26T06:59:08.640 回答