Relatorio em PDF com ireport

11 respostas
diogoprosoft

Galera preciso de uma ajuda de vcs!!!

Estou chamando um relatorio .jasper mais é para Desktop
meu codigo é esse

String pathJasper = getServlet().getServletContext().getRealPath(
                "/ireport/") + "/";

        String path = getServlet().getServletContext().getRealPath("/");

        Map parametros = new HashMap();

        parametros.put("PathRelatorios", pathJasper + "ExtratoParcelaCliente.jasper");
        try {

            JRResultSetDataSource jrRS = new JRResultSetDataSource(rs); 
            JasperPrint impressao = JasperFillManager.fillReport(pathJasper + "/ExtratoParcelaCliente.jasper", parametros, jrRS);
                       
            JasperViewer.viewReport(impressao);
        } catch (Exception e) {
            response.getWriter().println("Erro ao gerar o relatório: " + e);
        }

Preciso conveter para pdf e que seja para web não desktop

Como posso fazer isso?

TÍTULO EDITADO PELA MODERAÇÃO!!!

11 Respostas

jpcasati

se n me engano eh a mesma coisa so precisa dos jars do ireport no servidor. mas faz mto tempo q fiz q nem lembro :roll:

O
package Service.Relatorio;

import java.io.File;
import jdbc.ConnectionFactory;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.SQLException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class GeraRelatorioProcedure extends HttpServlet {
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String NomeJasper = "irptagruparecpag";
        String pathJasper = getServletContext().getRealPath("/WEB-INF/classes/jasper/")  + "/";
        String path = getServletContext().getRealPath("/");
        new File(path + NomeJasper + ".pdf").deleteOnExit();
        try {
            JasperPrint impressao = this.SetaValoresJasper(pathJasper + NomeJasper + ".jasper");
            if (impressao != null) {
                //Exporta para PDF
                JasperExportManager.exportReportToPdfFile(impressao, path + NomeJasper + ".pdf");
                //Exibe o seu PDF no Browser
                response.sendRedirect("http://Maquina:9090/RelatorioWEB/" + NomeJasper + ".pdf");

            }
        } catch (IOException ex) {
             response.getWriter().print("Erro");
        } catch (JRException ex) {
            response.getWriter().print("Erro2");
        }

    }
    
    public JasperPrint SetaValoresJasper(String CaminhoArquivoJar) {
        try {
            @SuppressWarnings("static-access")
            Connection con = ConnectionFactory.getInstance().getConnection();

            CallableStatement L_CallableStatement = con.prepareCall("{call sp_Rel_Agupamento_Mensal_Rec_Pag(1,2008,12)}");  
            ResultSet L_ResultSet = L_CallableStatement.executeQuery();  
            JRResultSetDataSource jrRS = new JRResultSetDataSource(L_ResultSet);
            HashMap map = new HashMap();
            JasperPrint rel = JasperFillManager.fillReport(CaminhoArquivoJar, map, jrRS);

            return rel;
        } catch (JRException ex) {
            return null;
        } catch (SQLException ex) {
            return null;
        }
    }

}
O

Veja se isso pode te ajudar.

diogoprosoft

Galera meu codigo esta assim agora

String pathJasper = getServlet().getServletContext().getRealPath("/ireport/") + "/";   
        String pathImages = getServlet().getServletContext().getRealPath("/ireport/") + "/";   
  
        String path = getServlet().getServletContext().getRealPath("/");   
        Map parametros = new HashMap();   
  
        parametros.put("SUBREPORT_DIR", pathJasper);   
        parametros.put("IMAGES_URI", pathJasper);   
  
        try {   
            JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
            JasperPrint impressao = JasperFillManager.fillReport(pathJasper + "/ExtratoParcelaCliente.jasper", parametros, jrRS);   
  
            JasperExportManager.exportReportToPdfFile(impressao,path + "/ireport/ExtratoParcelaCliente.pdf");   
  
            response.sendRedirect("/web/ireport/ExtratoParcelaCliente.pdf");   
  
        } catch (Exception e) {   
            response.getWriter().println("Erro ao gerar o relatorio: " + e);   
        }

o erro que esta acorrendo e esse

HTTP Status 404 - /web/ireport/ExtratoParcelaCliente.pdf

--------------------------------------------------------------------------------

type Status report

message /web/ireport/ExtratoParcelaCliente.pdf

description The requested resource (/web/ireport/ExtratoParcelaCliente.pdf) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.16

o que pode ser?

O

Ele criou o PDF no caminho que você pediu ?

diogoprosoft

Sim

D

Cria o PDF vazio?

diogoprosoft

Sim esta criando vazio

O

O que esta retornando a linha

JasperPrint impressao = JasperFillManager.fillReport(pathJasper + "/ExtratoParcelaCliente.jasper", parametros, jrRS);

O conteudo da variavel pathJasper esta certinho ?

diogoprosoft

Consegui pessoal

meu codigo ficou assim:

String pathJasper = getServlet().getServletContext().getRealPath("/ireport/") + "/";   
        String pathImages = getServlet().getServletContext().getRealPath("/ireport/") + "/";   
  
        String path = getServlet().getServletContext().getRealPath("/");   
        Map parametros = new HashMap();   
  
        parametros.put("SUBREPORT_DIR", pathJasper);   
        parametros.put("IMAGES_URI", pathJasper);   

       try {
            
            //JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
            //JasperPrint impressao = JasperFillManager.fillReport(pathJasper + "/ExtratoParcelaCliente.jasper", parametros, jrRS); 
            JasperPrint impressao = JasperFillManager.fillReport(pathJasper + "/ExtratoParcelaCliente.jasper", parametros,  orafactory.getConnection());
            
            byte[] bytes = JasperExportManager.exportReportToPdf(impressao);
            byte[] arquivo = bytes;
            String nomeDoArquivo = "ExtratoParcelaCliente.pdf";
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename=".concat(nomeDoArquivo));
            response.setContentLength(arquivo.length);
            ServletOutputStream sos = response.getOutputStream();
            sos.write(arquivo, 0, arquivo.length);
            sos.flush();
            sos.close();
            } catch (Exception e) {
            if(e.getClass().getSimpleName().equals("ClientAbortException")){
                    System.out.println("Documento cancelado");
            } else{
                    System.out.println("Outros");
            } 
            }

Valeu :smiley:

O

Parabéns qualque coisa estamos ai

Criado 8 de setembro de 2008
Ultima resposta 10 de set. de 2008
Respostas 11
Participantes 4