IReport Tibco - Error processing XML: no element found

1

Hello!

This is the following I'm having trouble generating the report, I'm using jsf, hibernate, mysql

Follow my bean.

@Named
@RequestScoped
public class RelatorioBean implements Serializable {


    private static final long serialVersionUID = 1L;

    private List<Funcionario> listaFuncionariosRel;
    private Funcionario funcionarioSelecionadosPDF;

    @Inject
    private Funcionarios funcionarios;

    @Inject
    private EntityManager manager;//

    @Inject
    private FacesContext facesContext;//

    @Inject
    private HttpServletResponse response;//

    public Funcionarios getFuncionarios() {
        return funcionarios;
    }

    public void setFuncionarios(Funcionarios funcionarios) {
        this.funcionarios = funcionarios;
    }

    public List<Funcionario> getListaFuncionariosRel() {
        return listaFuncionariosRel;
    }

    public void setListaFuncionariosRel(List<Funcionario> listaFuncionariosRel) {
        this.listaFuncionariosRel = listaFuncionariosRel;
    }

    public Funcionario getFuncionarioSelecionadosPDF() {
        return funcionarioSelecionadosPDF;
    }

    public void setFuncionarioSelecionadosPDF(Funcionario funcionarioSelecionadosPDF) {
        this.funcionarioSelecionadosPDF = funcionarioSelecionadosPDF;
    }

    public void exporterPdf() {//não estou usando este..método
        try {

            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext ec = facesContext.getExternalContext();
            HttpServletResponse response = (HttpServletResponse) ec.getResponse();

            //InputStream caminho = getClass().getResourceAsStream("/Ireport/func.jrxml");//acrescentei
            Map<String, Object> params = new HashMap<>();//.getCpf()

            params.put("cpf_Funcionario", funcionarioSelecionadosPDF.getCpf());

            /*response.reset();
            response.setContentType("application/pdf");//tbm
            response.setHeader("Content-disposition", "inline; filename=relatorio.pdf");*/
            ec.responseReset();
            ec.setResponseContentType("application/pdf");
            ec.setResponseHeader("Content-disposition", "inline; filename=relatorio.pdf");

            OutputStream saida = ec.getResponseOutputStream();

            GeradorDeRelatorios gerador = new GeradorDeRelatorios();
            gerador.geraPDF("/Ireport/func.jrxml", params, saida);

            JasperReport pathReport = JasperCompileManager.compileReport("/Ireport/func.jrxml");

            saida.close();
            facesContext.getResponseComplete();

        } catch (IOException ex) {
            FacesUtil.addErrorMessage("Erro 1: " + ex.getMessage());
        } catch (JRException ex) {
            FacesUtil.addErrorMessage("Erro 2: " + ex.getMessage());
        }
    }

    public void exportPdf2() {

        Map<String, Object> params = new HashMap<>();
        //params.put("cpf",this.listarDadosRel());
        params.put("cpf_Funcionario", this.funcionarioSelecionadosPDF);
        ExecutorReport execute = new ExecutorReport("/Ireport/func.jasper",
                this.response, params, "Funcionario.pdf");
        Session session = manager.unwrap(Session.class);
        session.doWork(execute);
        facesContext.responseComplete();

    }

}

My other ExecutorReport class

public class ExecutorReport implements Work {

    private String caminhoReport;
    private HttpServletResponse response;
    private Map<String,Object>params;
    private String nomeArquivo;

    public ExecutorReport(String caminhoReport, HttpServletResponse response, Map<String, Object> params, String nomeArquivo) {
        this.caminhoReport = caminhoReport;
        this.response = response;
        this.params = params;
        this.nomeArquivo = nomeArquivo;
    }



    @Override
    public void execute(Connection connection) throws SQLException {
        try{
        InputStream relatorioStream = this.getClass().getResourceAsStream(nomeArquivo);        
        JasperPrint print = JasperFillManager.fillReport(relatorioStream,this.params,connection);

        JRExporter  exportador = new JRPdfExporter();

        exportador.setParameter(JRExporterParameter.OUTPUT_STREAM,response.getOutputStream());
        exportador.setParameter(JRExporterParameter.JASPER_PRINT,print);

        response.setContentType("application/pdf");
        exportador.exportReport();

    }catch(Exception e ){
        throw new SQLException("erro ao gerar "+this.caminhoReport);
    }

    }

}

apache message

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type FacesContext with qualifiers @Default
    
asked by anonymous 04.11.2016 / 14:49

2 answers

1

He is understanding that his output is xhtml, should be PDF: response.setContentType("application/pdf");

And there should also be a header for your output:

            response.setHeader(
                "Content-disposition",
                "inline;filename="teste.pdf");

I can not test your implementation, but here's a complete example:

try {
        JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
        JasperReport jasperReport = JasperCompileManager
                .compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                jasperReport, parameters, conection);
        // Gera saida em PDF em uma servlet
        HttpServletResponse response = (HttpServletResponse) context
                .getExternalContext().getResponse();
        response.setContentType("application/pdf");
        Calendar gc = GregorianCalendar.getInstance();
        response.setHeader(
                "Content-disposition",
                "inline;filename=" + report + gc.get(Calendar.YEAR) + ""
                        + gc.get(Calendar.MONTH) + ""
                        + gc.get(Calendar.DAY_OF_MONTH) + ""
                        + gc.get(Calendar.HOUR_OF_DAY) + ""
                        + gc.get(Calendar.MINUTE) + ""
                        + gc.get(Calendar.SECOND) + ".pdf");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "No-cache");
        ServletOutputStream servletOutputStream = response
                .getOutputStream();

        JasperExportManager.exportReportToPdfStream(jasperPrint,
                servletOutputStream);

        servletOutputStream.flush();
        servletOutputStream.close();



    } catch (JRException ex) {
        throw new SystemException(ex);
    }
    context.responseComplete();
    
15.11.2016 / 00:45
0

Aew picked up ... I created a new class in the .util package

public class UtilFaces {

public UtilFaces() {
}
public FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
}
public ExternalContext getExternalContext() {
    return getFacesContext().getExternalContext();
}
public HttpServletRequest getRequest() {
    return (HttpServletRequest) getExternalContext().getRequest();
}
public HttpServletResponse getResponse() {
    return (HttpServletResponse) getExternalContext().getResponse();
}
public HttpSession getHttpSession(){
    return (HttpSession) getFacesContext().getExternalContext().getSession(false);
}
public ServletContext getServletContext(){
    return (ServletContext) getExternalContext().getContext();
}
public void msg(String destination,String msg){
    FacesMessage message = new FacesMessage(msg);
    getFacesContext().addMessage(destination, message);
}
public static void addErrorMessage(Exception ex, String defaultMsg) {
    String msg = ex.getLocalizedMessage();
    if (msg != null && msg.length() > 0) {
        addErrorMessage(msg);
    } else {
        addErrorMessage(defaultMsg);
    }
}
public static void addErrorMessages(List<String> messages) {
    for (String message : messages) {
        addErrorMessage(message);
    }
}
public static void addErrorMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
    FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
public static void addSuccessMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
    FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
}
public static void addWarnMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg);
    FacesContext.getCurrentInstance().addMessage("warnInfo", facesMsg);
}
public static void addFatalMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
    FacesContext.getCurrentInstance().addMessage("fatalInfo", facesMsg);
}

}

and I have updated the method that calls the report ...

aew the class that did catch

public class ExecutorReport implements Work {

private String caminhoRelatorio;
private HttpServletResponse response;
private Map<String,Object>parametros;
private String nomeArquivoSaida;

private boolean relatorioGerado;

public ExecutorReport(String caminhoRelatorio, HttpServletResponse response, Map<String, Object> parametros, String nomeArquivoSaida) {
    this.caminhoRelatorio = caminhoRelatorio;
    this.response = response;
    this.parametros= parametros;
    this.nomeArquivoSaida = nomeArquivoSaida;

    this.parametros.put(JRParameter.REPORT_LOCALE, new Locale("pt", "BR"));
}



@Override
public void execute(Connection connection) throws SQLException {
    try{
    InputStream relatorioStream = this.getClass().getResourceAsStream(this.caminhoRelatorio);    
    JasperPrint print = JasperFillManager.fillReport(relatorioStream,this.parametros,connection);
     this.relatorioGerado = print.getPages().size()>0;

      if(this.relatorioGerado){
          Exporter<ExporterInput,PdfReportConfiguration,PdfExporterConfiguration,
                  OutputStreamExporterOutput> exportador = new JRPdfExporter();
                  exportador.setExporterInput(new SimpleExporterInput(print));
                  exportador.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));

                  response.setContentType("application/pdf");
                  response.setHeader("Content-Disposition","attachment; filename=\""
                  +this.nomeArquivoSaida + "\"");
                  exportador.exportReport();

}         } catch (Exception e) {             throw new SQLException ("Error running report" + this.pathReport, e);         }     }

public boolean isRelatorioGerado() {
    return relatorioGerado;
}

}

    
29.11.2016 / 14:15