论坛首页 Java企业应用论坛

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

浏览 3312 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-01  
在浏览器生成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);
	}

}
   发表时间:2009-07-20  
很奇怪,在ff中可以正常生成pdf,在ie6则在网页上显示乱麻。
0 请登录后投票
   发表时间:2010-03-12  
不太详细啊,怎么在web容器里部署啊,
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics