-1

I am using Jasper report with java. I want to make the export file ask for the location to download when launching it as web app.

What i am doing now is. I am giving a path for the file(PDF,DOCX,XLS) to be exported in a location. What i need is, the browser should make a popup and ask for the file location to be downloaded just like firefox.

I've done it in a java application, by providing a path,

Connection conn = null;
    ResultSet rs = null;
    JasperReport jasperReport = null;
    JasperPrint print = null;
    String filename = "Report.pdf";
    String query = "{CALL get_report_data()}";
    try {
        if (conn == null) 
        {
            String hostName = "localhost";
            String dbName = "test";
            String userName = "root";
            String password = "root";
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String connectionURL = "jdbc:mysql://" + hostName + ":3306/" + dbName;
                conn = DriverManager.getConnection(connectionURL, userName, password);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        CallableStatement cstmt = conn.prepareCall(query);

        rs = cstmt.executeQuery();

        JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);

        jasperReport = JasperCompileManager
                .compileReport("E:\\Eclipse 2019-03 Workspace\\Report\\static_land_report.jrxml");

        Map<String, Object> parameters = new HashMap<String, Object>();

        print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);

JasperExportManager.exportReportToPdfFile(print, "E:\Eclipse 2019-03 Workspace\Report\static_land_report.pdf");

Now, i want the above code to be done in web app, and without providing the path.

alexander
  • 11
  • 6
  • Are you using Jasper reports server or are you developing your own interface? – Petter Friberg Sep 18 '19 at 06:17
  • My idea is to create a dynamic web project, and make the report getting generated at the browser itself. (without providing the location of the download path) – alexander Sep 19 '19 at 05:07
  • Why not use standard way? If you send PDF to browser it will download it as any other doc. Anyway this does not seem very related to Jasper report it is more about how browser works – Petter Friberg Sep 19 '19 at 06:07
  • Related https://stackoverflow.com/questions/6520231/how-to-force-browser-to-download-file and if you like to view in browser export to HTML and serve that – Petter Friberg Sep 19 '19 at 06:09

2 Answers2

0

Give a try as per the below steps.I found it from jaspersoft community.

  • Go to the repository tab in iReport

  • Select the folder in which you save your input controls

  • right click the folder, select add -> inputcontrol

  • In the id field, put your parameter name (it has to be exact to the value in between the $P{}

  • Give it a prompt name

  • In the input control details tab, select a single value, and select a data type (you can create a data type in the same manner in iReport)

  • Then go to the folder of your report, and on the input, control map right-click and select link an existing input control, and select the input control you just created.

Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
0

thanks for reaching out. But, i found a solution.

package com.report.java;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public class PdfReportDownload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public PdfReportDownload() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try 
        {
            generatePdfReport(response);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void generatePdfReport(HttpServletResponse response) 
    {
        try 
        {
            Connection conn=null;
            ResultSet rs=null;
            JasperReport jasperReport = null;
            String query ="{CALL get_report_data()}";
            try 
            {   
                if(conn==null)
                {
                    try 
                    {
                        conn= MySQLConnection.getConnection();

                    } 
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }   
            CallableStatement cstmt = conn.prepareCall(query);

            rs = cstmt.executeQuery();

            JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);  

            jasperReport = JasperCompileManager.compileReport("E:\\Eclipse 2019-03 Workspace\\Report_Download\\static_land_report.jrxml");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap <String, Object>(), resultSetDataSource);

            JRPdfExporter pdfExporter = new JRPdfExporter();
            pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            ByteArrayOutputStream pdfReportStream = new ByteArrayOutputStream();
            pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfReportStream));
            pdfExporter.exportReport();

            response.setContentType("application/pdf");
            response.setHeader("Content-Length", String.valueOf(pdfReportStream.size()));
            response.addHeader("Content-Disposition", "attachment; filename=Report.pdf;");

            OutputStream responseOutputStream = response.getOutputStream();
            responseOutputStream.write(pdfReportStream.toByteArray());
            responseOutputStream.close();
            pdfReportStream.close();
        } 
        catch (Exception e) 
        {
                System.out.println(e);
        }
        }
        catch(Exception e) 
        {
            System.out.println(e);
        }
    }

}

On invoking this servlet, the report gets download

alexander
  • 11
  • 6