Java PDF Report Error

Asked

Viewed 187 times

4

I have a little java program for PDF reporting. The problem is that every time I send the PDF via Email, it opens the PDF on the screen. Where can I change, so it doesn’t open on the screen when sending by Email? follow my code:

public class Main {
   public static void main(String args[]) {
    String dirConf = args[0];

    if ((new File(dirConf)).exists())
        try {
            Map<String, String> param = (new ReadXML()).getParams(dirConf);
            server = (String) param.get("server");
            port = (String) param.get("port");
            database = (String) param.get("database");
            usuario = (String) param.get("user");
            senha = (String) param.get("password");
            url = (String) param.get("url");
            type = (String) param.get("type");
            folder = (String) param.get("folder");
            primary = (String) param.get("primary");
            secundary = (String) param.get("secundary");
            idiomas.add(primary);
            idiomas.add(secundary);
            name = getName(url);
            destFileName = (new StringBuilder(String.valueOf(folder)))
                    .append(name).append((String) param.get("id"))
                    .append(".").append(type).toString();
            sourceFileName = url;
            connection = (new ConectaProgress()).conectar(server, port,
                    database, usuario, senha);
            tempSet = param.keySet();
            Object object;
            for (Iterator<String> iterator = tempSet.iterator(); iterator
                    .hasNext(); paramJasper.put(object.toString(),
                    (String) param.get(object)))
                object = iterator.next();

            for (Iterator<String> iterator1 = idiomas.iterator(); iterator1
                    .hasNext();) {
                String string = (String) iterator1.next();
                label = (new DAO()).carregaLabel(connection, string);
                tempSet = label.keySet();
                String string1;
                for (Iterator<String> iterator2 = tempSet.iterator(); iterator2
                        .hasNext(); paramJasper.put(string1,
                        (String) label.get(string1)))
                    string1 = (String) iterator2.next();

            }

            (new Main()).exportar(type, destFileName, sourceFileName,
                    paramJasper);
            File destFile = new File(destFileName);
            if (destFile.exists())
                desktop.open(destFile);
            System.out.println("ok\nrelatorio gerado com sucesso.");
        } catch (IOException e) {
            throw new Excecao(e);
        }
    else
        throw new Excecao(new Exception("arquivo nao encontrado."));
}

private static String getName(String url) {
    char name[] = (new File(url)).getName().toCharArray();
    url = "";
    char ac[];
    int j = (ac = name).length;
    for (int i = 0; i < j; i++) {
        char c = ac[i];
        if (c == '.')
            break;
        url = (new StringBuilder(String.valueOf(url))).append(c).toString();
    }

    return url;
}

private void exportar(String type, String destFileName,
        String sourceFileName, Map<String, String> paramJasper)
        throws IOException {
    paramJasper.put("icn",
            getClass().getClassLoader().getResource("icn.png")
                    .toString());

    paramJasper.put("logo",
            getClass().getClassLoader().getResource("logo.jpg")
                    .toString());
    JasperPrint relatorioFinal = null;
    FontFactory.register(getClass().getClassLoader()
            .getResource("ARIALUNI.TTF").toString());
    try {
        relatorioFinal = JasperFillManager.fillReport(sourceFileName,
                paramJasper, connection);
    } catch (Exception e) {
        throw new Excecao(e);
    }
    if (type.equals("pdf"))
        try {
            JasperExportManager.exportReportToPdfFile(relatorioFinal,
                                    destFileName);

        } catch (Exception e) {
            e.printStackTrace();
            throw new Excecao(e);
        }
    if (type.equals("xls"))
        try {
            JRXlsExporter xls = new JRXlsExporter();
            xls.setParameter(JRXlsExporterParameter.JASPER_PRINT,
                    relatorioFinal);
            xls.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME,
                    destFileName);
            xls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
                    Boolean.FALSE);
            xls.setParameter(
                    JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
                    Boolean.FALSE);
            xls.setParameter(
                    JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
                    Boolean.TRUE);
            xls.exportReport();
        } catch (JRException e) {
            throw new Excecao(e);
        }
    if (type.equals("csv"))
        try {
            JRCsvExporter csv = new JRCsvExporter();
            csv.setParameter(JRXlsExporterParameter.JASPER_PRINT,
                    relatorioFinal);
            csv.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME,
                    destFileName);
            csv.exportReport();
        } catch (JRException e) {
            throw new Excecao(e);
        }
    if (type.equals("ods"))
        try {
            JROdsExporter ods = new JROdsExporter();
            ods.setParameter(JRXlsExporterParameter.JASPER_PRINT,
                    relatorioFinal);
            ods.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME,
                    destFileName);
            ods.exportReport();
        } catch (JRException e) {
            throw new Excecao(e);
        }

    if (type.equals("pre")) {
        JasperViewer jv = new JasperViewer(relatorioFinal);
        jv.setExtendedState(6);
        jv.setTitle(" RelatorioPdf");
        java.awt.image.BufferedImage imagem = ImageIO.read(getClass()
                .getClassLoader().getResource("imagem.png"));
        jv.setIconImage(imagem);
        jv.setVisible(true);

    }

}
  • 6

    Did you develop the code? I believe in if (destFile.exists()) desktop.open(destFile); that is your "problem", because it does this check after generating the file.

  • @user21886 found some solution?

2 answers

1

Expensive,

At the end of the Try has the commands:

File destFile = new File(destFileName);
if (destFile.exists())
     desktop.open(destFile); <--- este principalmente.

They serve to open the file by operating system.

Looking at the export method, if you pass the pre type, it will also present the window (the JasperViewer that’s what it’s for):

if (type.equals("pre")) {
    JasperViewer jv = new JasperViewer(relatorioFinal);
    jv.setExtendedState(6);
    jv.setTitle(" RelatorioPdf");
    java.awt.image.BufferedImage imagem = ImageIO.read(getClass()
            .getClassLoader().getResource("imagem.png"));
    jv.setIconImage(imagem);
    jv.setVisible(true);

}

0

Remove from this

 if (destFile.exists())
                desktop.open(destFile);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.