简体   繁体   中英

How to display a JasperReports pdf file in an iframe?

in currently I run my pdf report using JasperReports when clicking on report button the pdf report run with another browser tab but now I want to run the report on my JSP page using iframe tag. please help me to view / embedded jasper report on my web page.................................

everybody can give me an example.

Html code is

  <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>

my js function/report calling function

    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);
            }
        }
    }
}

just add this line in js function instate of "window.open(ajaxURL,'_blank');" line

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM