11:32

Reportes HTML con JasperReport desde un servlet.

posted under by sysdent | Edit This














Hace poco comenze a trabajar reportes en JasperReports y realmente me ha convensido de ser una opción muy poderosa a la hora de reportear en nuestras aplicaciones.

Una de las caracteristicas que más me han gustado es la idea de generar una plantilla xml (ó mejor jrxml) y luego poder generar el reporte tanto en PDF como en HTML, sin embargo lo que no había podido lograr era exportar desde un servlet el html. Veamos un ejemplo de como se debería hacer:

public void exportToHTMLStream(OutputStream out, String contextPath,
HttpServletRequest request, JasperPrint print) throws JRException, IOException {
final JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.exportReport();
}

Con esto lograremos solucionar el problema de generar el reporte en HTML, pero nos encontraremos con algunos problemitas:

  1. El reporte no carga algunas imagenes que usa Jasper para alinear el contenido.
Para esto tenemos 2 opciones:

Establecer el parámetro IS_USING_IMAGES_TO_ALIGN de jasper a false.
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
Boolean.FALSE);
Esto nos soluciona el problema de las imagenes transparentes que crea jasper para alinear el contenido, sinembargo, que pasa si mis reportes tiene graficas de estadisiticas de los datos que tampoco se muestran?

Esto ocurre por que lo que hace Jasper es generar tags img src= "Ruta de la imagen", y las imagenes no se encuentran.

Sin embargo Jasper permite una solución a este problema, y es gracias al Servlet ImageServlet.

Lo que debemos hacer es registrar un nuevo servlet en nuestro web.xml así:


JasperImageServlet
net.sf.jasperreports.j2ee.servlets.ImageServlet


JasperImageServlet
/jasperImage


y posteriormente indicarle al exportador que este servlet será el encargado de generar las imagenes del reporte así:

exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, contextPath
+ "/jasperImage?image=");


ahora muestro como quedaría nuestro método para exportar reportes a HTML con imagenes.

public void exportToHTMLStream(OutputStream out, String contextPath,
HttpServletRequest request) throws JRException, IOException {
final JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, contextPath
+ "/jasperImage?image=");
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
Boolean.FALSE);
request.getSession().setAttribute(
ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
exporter.exportReport();
}

Listo, con esto quedará solucionado nuestro problema.

cualquier comentario, comentario o duda será atendida con gusto.

1 comentarios

Make A Comment
top