This article provides a brief howto on how to display PDF reports generated with JasperReports from a web application to the user's browser. Basic knowledge of JasperReports and Java Servlet programming is assumed. See the Resources section for links to JasperReports and Servlet programming tutorials and documentation.
The trick to sending a PDF report generated by JasperReports to the
user's browser is to call the net.sf.jasperreports.engine.JasperRunManager.runReportToPdf()
method. That method has several overloaded versions, the one we will use
here has three parameters, a String
representing the
absolute path of the compiled report (jasper file), an instance of a
class implementing the java.util.Map
interface, and an
instance of a class implementing the net.sf.jasperreports.engine.JRDataSource
interface. The JasperRunManager.runReportToPdf()
method
returns an array of bytes that can be passed as a parameter to the write()
method of the javax.servlet.ServletOutputStream
class. An
instance of ServletOutputStream
can be obtained from the getOutputStream()
method of the javax.servlet.http.HttpServletResponse
class.
The best way to explain and visualize all of this is by example, the
following code segment demonstrates this technique:
package net.ensode.jasperreportsbrowserdemo;
import java.io.File;
import java.io.IOException;
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 JasperReportsBrowserDemoServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletOutputStream servletOutputStream = response.getOutputStream();
File reportFile = new File(getServletConfig().getServletContext()
.getRealPath("/reports/Simple_Report.jasper"));
byte[] bytes = null;
try
{
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),
new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
}
catch (JRException e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
As can be seen in the example, the easiest way to obtain the absolute
path of the jasper file is to call the getRealPath()
method
of an instance of a class implementing the javax.servlet.ServletContext
interface. For our simple example, we pass an empty instance java.util.HashMap
and an instance of net.sf.jasperreports.engine.JREmptyDataSource
as the other two parameters to the JasperRunManager.runReportToPdf()
method, more complex applications would pass some data inside these two
parameters.