首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > PowerDesigner >

JasperReport学习札记3-在浏览器生成PDF文件

2012-11-08 
JasperReport学习笔记3-在浏览器生成PDF文件在浏览器生成PDF文件,主要用了JasperRunManager对象面的runRep

JasperReport学习笔记3-在浏览器生成PDF文件
在浏览器生成PDF文件,主要用了JasperRunManager对象面的runReportToPdfStream方法,在WEB里一般都先用iReport生成jasper文件,这样就去解析JRXML文件了

例子如下

import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.util.HashMap;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.jasperreports.engine.JREmptyDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JasperRunManager;public class JasperReportServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//获得输出流ServletOutputStream outputStream = response.getOutputStream();//获得输入流InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("JasperFile//JasperReportFile.jasper");try {//生成网页的PDF文件JasperRunManager.runReportToPdfStream(inputStream, outputStream, new HashMap(), new JREmptyDataSource());//设置PDF格式response.setContentType("application/pdf");outputStream.flush();outputStream.close();} catch (JRException e) {StringWriter stringWriter = new StringWriter();PrintWriter printWriter = new PrintWriter(stringWriter);e.printStackTrace(printWriter);response.setContentType("text/plain");response.getOutputStream().print(stringWriter.toString());}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}}
1 楼 tom&jerry 2009-07-20   很奇怪,在ff中可以正常生成pdf,在ie6则在网页上显示乱麻。 2 楼 kuskyfei 2010-03-12   不太详细啊,怎么在web容器里部署啊, 3 楼 handsomestar 2010-03-15   mark 

热点排行