Home » Java » force download PDF with iReport and JSP

force download PDF with iReport and JSP

Posted by: admin July 15, 2018 Leave a comment

Questions:

please help, i want force download pdf file from JSP (generate from ireport) but the pdf file is blank document.

this is my source:

<%@page import="java.io.FileOutputStream"%>
<%@page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.JRExporter"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="com.sun.xml.internal.ws.message.ByteArrayAttachment"%>
<%@page import="net.sf.jasperreports.engine.export.JRPdfExporter"%>
<%@page import="net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import="java.util.HashMap"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JasperReport"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Pagess</title>
    </head>
    <body>
        <%response.setHeader("Content-Transfer-Encoding", "binary");
         response.setHeader("Content-Disposition", "attachment; filename='Report.pdf'"); %>
        <% Class.forName("com.mysql.jdbc.Driver"); %>
        <% Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db_penjualan", "root", ""); %>
        <% HashMap map = new HashMap();
            map.put("KodeKonsumen", "k001");
        JasperPrint print = JasperFillManager.fillReport(application.getRealPath("LaporanDataKonsumen.jasper"), map, con);
JasperExportManager.exportReportToPdf(print);          %>
    </body>
</html>

when i write code like this,

<%@page import="java.io.FileOutputStream"%>
<%@page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.JRExporter"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="com.sun.xml.internal.ws.message.ByteArrayAttachment"%>
<%@page import="net.sf.jasperreports.engine.export.JRPdfExporter"%>
<%@page import="net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import="java.util.HashMap"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JasperReport"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="application/pdf"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Pagess</title>
    </head>
    <body>
        <% Class.forName("com.mysql.jdbc.Driver"); %>
        <% Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db_penjualan", "root", ""); %>
        <% 
        HashMap map = new HashMap();
            map.put("KodeKonsumen", "k001");
        JasperPrint print = JasperFillManager.fillReport(application.getRealPath("LaporanDataKonsumen.jasper"), map, con);
        JasperExportManager.exportReportToPdfStream(print, response.getOutputStream());  %>
    </body>
</html>

the pdf file just display on browser, not downloaded
enter image description here
i want the pdf file force download, please help. thanks before

Answers: