如何在iframe中显示JasperReports pdf文件?

时间:2017-10-29 08:47:03

标签: javascript iframe

目前我在点击报告按钮时使用JasperReports运行我的pdf报告,pdf报告与另一个浏览器选项卡一起运行,但现在我想使用iframe标签在我的JSP页面上运行报告。请帮我在我的网页上查看/嵌入jasper报告.................................

每个人都可以举个例子。

Html代码

  <div class="col-md-12">
                <!-- begin panel -->
                <div class="panel panel-inverse" data-sortable-id="form-stuff-2">
                    <div class="panel-heading">
                        <div class="panel-heading-btn">
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-default" data-click="panel-expand"><i class="fa fa-expand"></i></a>
                        </div>
                        <h4 class="panel-title">Ship Service Report Page </h4>
                    </div>
                       <div class="panel-body">
                        <form class="form-horizontal">

                           <div class="form-group">
                                <label class="col-md-2 control-label">Ship Name</label>
                                <div class="col-md-4">
                                  <select name="" class="form-control" id="shipNam" name="shipNam">
                                        <option value="" selected="selected">Select Ship name</option>
                                        <s:iterator value="reportInfos" status="rowstatus">
                                        <option value="<s:property value='shipNo' />"><s:property value='shipName' /></option>
                                        </s:iterator>
                                    </select>
                                </div>
                                  </div>
                             <div class="form-group">  
                                <label class="col-md-2 control-label">From Date</label>
                                <div class="col-md-3">
                                    <input type="text" class="form-control" name="fromDate" id="fromDate" placeholder="Select Date" value="" />
                                </div>
                            </div>
                          <div class="form-group">  
                                <label class="col-md-2 control-label">To Date</label>
                                <div class="col-md-3">
                                    <input type="text" class="form-control" name="toDate" id="toDate" placeholder="Select Date" value="" />
                                </div>
                           </div>

                            <div class="form-group col-md-6">
                                <label class="col-md-4 control-label">Report Format</label>
                                <div class="col-md-8">
                                    <label class="radio-inline">
                                        <input type="radio" name="optionsRadios" id="option1" value="pdf" checked />
                                        PDF
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="optionsRadios" id="option2" value="xls" />
                                        Excel
                                    </label>
                                </div>
                          </div>
                            <div class="form-group">                                  
                                <div class="col-md-6">
                                    <button type="button" class="btn btn-sm btn-success" onclick="getShipServiceReport('shipNam','toDate' , 'fromDate')">View</button>
                                </div>
                            </div>
                        </form>
                    </div>
             </div>
                <!-- end panel -->
       </div>

我的js功能/报告调用功能

    function getShipServiceReport(ship_no,to_date, fromDate){ 
        var shipNos=document.getElementById(ship_no).value;
        var toDate=document.getElementById(to_date).value;
        var fromDate=document.getElementById(fromDate).value;
        var fileType=$('input[name="optionsRadios"]:checked').val();
        var ajaxURL = "getShipServiceReportRT.do?ship_no="+shipNos+"&to_date="+toDate+"&fileType="+fileType+"&fromDatee="+fromDate;          
        window.open(ajaxURL,'_blank');
 //$("#documentArea.html('<iframe src='"+ajaxURL+"' frameBorder='0' style='height:1200px;width:100%;'></iframe>')"); 

    }

Java Action class

public void getShipServiceReport()throws JRException, ServletException, IOException{
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("shipNo", ship_no);
    parameters.put("fromDate", fromDatee);
    parameters.put("toDate", to_date);
    System.out.println("Parameter"+ship_no);
    String filename="shipServiceInformation.jasper";
    String reportType=fileType;
    showReport("",parameters,filename,reportType);

    }

public static void showReport(String key, Map<String, Object> parameters, String filename, String reportType)
        throws JRException, ServletException, IOException {
    Connection connection = null;
    HttpServletResponse response = ServletActionContext.getResponse();
    String outputFileName = "report_" + key;

    connection = DBC.openCon();

    ServletContext context = ServletActionContext.getServletContext();
    String path = context.getRealPath("/");

    JasperPrint jasperPrint = JasperFillManager.fillReport(path + "jasper" + "/" + filename, parameters,
            connection);
    OutputStream ouputStream = response.getOutputStream();
    JRExporter exporter = null;

    if ("pdf".equalsIgnoreCase(reportType)) {

        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType);
        exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
        System.out.println("Report Created");
    } else if ("rtf".equalsIgnoreCase(reportType)) {

        response.setContentType("application/rtf");
        response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType);
        exporter = new JRRtfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);

    } else if ("html".equalsIgnoreCase(reportType)) {

        exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);

    } else if ("xls".equalsIgnoreCase(reportType)) {

        response.setContentType("application/xls");
        response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType);
        exporter = new JRXlsExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);

    } else if ("csv".equalsIgnoreCase(reportType)) {

        response.setContentType("application/xls");
        response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType);
        exporter = new JRCsvExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
    }

    try {
        // JasperViewer.viewReport(jasperPrint, true); // for direct print
        exporter.exportReport();

    } catch (JRException e) {
        System.out.println(e);
    } finally {
        if (ouputStream != null) {
            try {
                ouputStream.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需在“window.open(ajaxURL,'_ blank');”的js函数实例中添加此行。线

$('#documentArea').html("<iframe name='your_frame_name' src='"+ajaxURL+"' height=100% width=100% > </iframe>")