jeudi 5 juillet 2018

JasperReport - Generate a Report as a Image for Download via web

Is there any method for generate Jasper Reports directly as a image file (.jpg,.png,.bmp,...), without the necessity of passing hashmap parameters to run a JasperReport as a pdf file for Download via web?

This is a section of code what i'm doing:

@RequestMapping(value = "/GenerarNota", method = RequestMethod.GET)
public @ResponseBody
void GenerarNota(HttpServletRequest req, HttpServletResponse response) throws JRException, IOException, SQLException {

    DataSource data = SessionFactoryUtils.getDataSource(session);
    HttpServletRequestWrapper srw = new HttpServletRequestWrapper(req);

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(srw.getServletContext().getRealPath(RepRutas.RUTA_NOTA)));
    Map parametros = new HashMap();

    parametros.put("idPaciente", Integer.parseInt(req.getParameter("idPaciente")));
    parametros.put("idResumen", Integer.parseInt(req.getParameter("idResumen")));
    parametros.put("tokenNota", Integer.parseInt(req.getParameter("tokenNota")));
    parametros.put("RutaRelativa", srw.getServletContext().getRealPath(RepRutas.RUTA_RELATIVA));
    parametros.put("idMedico", Integer.parseInt(Sistema.getIdUsuario(req)));

    byte[] reporteBytes = JasperRunManager.runReportToPdf(jasperReport, parametros, data.getConnection());
    response.setContentType("application/pdf");
    response.setHeader("Content-disposition", "inline; filename=Nota_" + req.getParameter("idPaciente") + ".pdf");
    response.setHeader("Cache-Control", "max-age=30");
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setContentLength(reporteBytes.length);

    ServletOutputStream out = response.getOutputStream();
    out.write(reporteBytes, 0, reporteBytes.length);
    out.flush();
    out.close();

}




Aucun commentaire:

Enregistrer un commentaire