使用带 JSP 的 Ireport 生成 PDF

Generate PDF using Ireport with JSP

我想通过 JSP

使用 JasperReports 生成 PDF 报告

这是我的代码

<% 
Class.forName("oracle.jdbc.OracleDriver");
Connection cn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:personnel","user","password");

try {
   File reportFile = new File(application.getRealPath("//reports//rapport_absences.jasper"));
    Map h = new HashMap();

    byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),h,cn);

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition",  "inline; filename=report.pdf ");
    response.setContentLength(bytes.length);
    ServletOutputStream outStream = response.getOutputStream();
    outStream.write(bytes,0,bytes.length);
    outStream.flush();
    outStream.close();

}catch (Exception ex){
    ex.printStackTrace();
}
%>

但它给了我一个空白的 PDF,虽然当我 运行 来自 Ireport 的报告时它 运行 很好并获取数据但是从 jsp 它得到一个空白的 pdf

一如既往,我找到了自己的解决方案,因为现在有人可以提供帮助,这就是它,如果将来有人需要它,我会把它放在这里

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page contentType="application/pdf" %>
<%@ page trimDirectiveWhitespaces="true"%>



<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.util.*" %>


<%
Class.forName("oracle.jdbc.OracleDriver");
Connection cn =    DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:personnel","charif","charif123");

response.setHeader("Content-Disposition",  "inline; filename=report.pdf ");
JasperPrint print = JasperFillManager.fillReport("D:\ISTA\Projet_Stage\GestionPersonnel\web\reports\rapport_absences.jasper",
new HashMap(),
cn);


JasperExportManager.exportReportToPdfStream(print,    
response.getOutputStream());

%>