通过Servlet或JSP将JasperReport的报告导出为pdf [英] Export JasperReport's report to pdf via Servlet or JSP

查看:489
本文介绍了通过Servlet或JSP将JasperReport的报告导出为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将jasper报告导出到使用JSP的pdf文件。

I have used the following code for exporting a jasper report to a pdf file using JSP.

response.setHeader("Content-Disposition", "inline; filename=\"application.pdf\"");
response.setContentType("application/pdf");

Connection con;

String host = "jdbc:mysql://localhost:3306/123";
String uname = "root";
String upass = "";
Class.forName("com.mysql.jdbc.Driver");      

con = DriverManager.getConnection(host, uname, upass);

String report="C:\\Users\\Acer\\Documents\\NetBeansProjects\\jasper\\1.jrxml";
JasperReport jr= JasperCompileManager.compileReport(report);
JasperPrint jasperPrint = JasperFillManager.fillReport(jr,  null, con);

JasperExportManager.exportReportToPdfFile(jasperPrint, "application.pdf");

当我在 NetBeans 中运行此JSP文件时,会出现一个消息框,其中包含浏览器中出现以下错误:

When I run this JSP file in NetBeans, a message box with the following error appears in the browser:

文件不以'%PDF-'开头。
Local\EWH _)!50gc#

"File does not begin with '%PDF-'. Local\EWH_)!50gc#"

此消息来自Adobe Reader。

This message comes from Adobe Reader.

任何帮助将不胜感激。

Any help would be appreciated.

推荐答案

使用 exportReportToPdfFile()是完全错误的:你不想要创建文件,而不是导出到响应流。因此,请使用以下代码替换代码段的最后一行:

Using exportReportToPdfFile() is totally wrong: you do not want to create a file, rather export to the response stream. So replace the last line of the snippet with:

OutputStream outStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);

这篇关于通过Servlet或JSP将JasperReport的报告导出为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆