Skip to content

Instantly share code, notes, and snippets.

@levymoreira
Created September 12, 2013 11:50
Show Gist options
  • Save levymoreira/6536181 to your computer and use it in GitHub Desktop.
Save levymoreira/6536181 to your computer and use it in GitHub Desktop.
Open report of iReport with JSF app.
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
FuncUtil.apagarArquivos(request);
String pathJasper = request.getRealPath("WEB-INF/classes/relatorio"); // local onde esta o relatorio
String os = System.getProperty("os.name");
File pasta = new File((os.toLowerCase().indexOf("linux") > -1 ? "/" : "\\"));
pathJasper += pasta;
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(pathJasper + "/NOME_DO_RELATORIO_COMPILADO.jasper");
if (jasperReport == null)
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao gerar ...", null));
}
Map parameters = new HashMap();
parameters.put("parametro1", 14); //parametros q serao enviados
String urlBd ="url conexao", userBd = "postgres", senhaBd = "postgres";
Connection con = (Connection) DriverManager.getConnection(urlBd, userBd, senhaBd);
JasperPrint impressao = JasperFillManager.fillReport(jasperReport, parameters, con);
String pathPdf = "/temp/" //crie uma pasta temp dentro de webContent caso nao exista para gerar os relatorios nela
+ request.getRequestedSessionId()
+ String.valueOf(Math.random() * 10000)
+ "DESCRICAO_QUALQUER_PARA_O_PDF_COLOQUE_O_NOME_DO_REL_PRA_FACILITAR"
+ ".pdf";
JasperExportManager.exportReportToPdfFile(impressao, request.getRealPath("") + pathPdf);
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect(request.getContextPath() + pathPdf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment