How to display DynamicReports in browser without download to cliend drive?

爱⌒轻易说出口 提交于 2019-12-25 07:18:49

问题


I have to display some reports with Dynamic Reports. I use NetBeans and Tomcat 7. Eventually, all must be uploaded to cloud OpenShift. I used DynamicReports to create simple report (code snippet):

    Connection conn=null;
    try {
    Class.forName(DBConnStrings.driver);
    conn = DriverManager.getConnection(DBConnStrings.url + DBConnStrings.dbName+DBConnStrings.sslState, DBConnStrings.userName, DBConnStrings.password);
    } catch (Exception e) {
        e.printStackTrace();

    }
    JasperReportBuilder report = DynamicReports.report();
    report
            .columns(
                    Columns.column("Tank Id", "id", DataTypes.integerType()),
                    Columns.column("Tank Name", "name", DataTypes.stringType()),
                    Columns.column("Label", "label", DataTypes.stringType()),
                    Columns.column("Description", "descrshort", DataTypes.stringType()));
    report.setDataSource("SELECT id, name, label, descrshort  FROM "+ DBConnStrings.dbName +".tbltankslist", conn);

    try {
            //show the report
    //report.show();

            //export the report to a pdf file
    report.toPdf(new FileOutputStream("c:/report.pdf"));
} catch (DRException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

This code located in a Servlet. It works. I get JasperViewer at first and a report.pdf on my HDD. But I don't want it. First I do not want to see JasperViewer, second I do not want to download file to client HDD. How to display report inside web-browser only?

Here is the question Jasper Reports. It is about jasper reports + iReport and I have no idea how to use that information for DynamicReports - at first, second there is also "download pdf to client drive" approach, but I need to show it inside the browser.


回答1:


use the following code in your file which redirect towards jasper invocation page, so that your jasperPDF should open in new tab instead of downloading.

JasperInvocation.jsp => file in which you invoke jasperReport

<form method="POST" action="JasperInvocation.jsp" target="_blank">



回答2:


Please find following code , I have implemented in Dynamic report(Jasper Api) , Its working for me :-

@RequestMapping(value="/pdfDownload", method = RequestMethod.GET)
public void getPdfDownload(HttpServletResponse response) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

report().columns().setDataSource().show()
.toPdf(buffer);

byte[] bytes = buffer.toByteArray();
InputStream inputStream = new ByteArrayInputStream (bytes);
IOUtils.copy(inputStream, response.getOutputStream());
response.setHeader("Content-Disposition", "attachment; filename=Accepted1.pdf");
response.flushBuffer();

}


来源:https://stackoverflow.com/questions/38074933/how-to-display-dynamicreports-in-browser-without-download-to-cliend-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!