views:

63

answers:

1

I am trying to call BIRT API in linux environment.

My source is an XML file and i create html, xml and pdf reports on that source XML.

My html and xml reports generates properly, but PDF report is created with size '0' (no contents).

However if i run the same program from a windows system, PDF report is created properly.

Is BIRT having platform dependent API?

I am using following code...

public static void generateReport(String format, String birtPath,
        String outputFile, String templateLoc, boolean log, File dataSource, boolean isSummaryReport)
        throws Exception {

    File outputFileObj = new File(outputFile + format);

    if (outputFileObj.exists()) {
        if (!outputFileObj.delete()) {
            Main
                    .getLogger()
                    .log(
                            "Error: A File with the same name '"
                                    + outputFileObj.getName()
                                    + "' exists in the result folder and it can't be overwritten."
                                    + LINE_SEPARATOR
                                    + "If it is opened then close it before generating the report.");
            ReportingUIErrorWriter.process(ReportingUIErrorProcessor
                    .getString("ERR_CODE_024"),
                    new String[] { outputFileObj.getName() });
            systemExit(FAILURE);
        }
    }

    /* Partie initialisation */
    ReportEngine engine = null;
    EngineConfig engineConfig = new EngineConfig();
    engineConfig.setEngineHome(birtPath);
    File logDir = new File(strLogDir);
    if (!logDir.exists()) {
        logDir.mkdir();
    }
    if (log) {
        engineConfig.setLogConfig(strLogDir, Level.INFO);
    } else {
        engineConfig.setLogConfig(strLogDir, Level.OFF);
    }

    try {
        engine = new ReportEngine(engineConfig);
        // Dont remove this code.If birt engine path is valid but it does
        // not contain
        // Birt Engine then it will validate Report engine object.
        engine.getRootScope();
    } catch (Exception e) {
        e.printStackTrace();
        Main
                .getLogger()
                .log(
                        "Error: Reporting engine path does not contain valid Birt Engine.");
        ReportingUIErrorWriter.process(ReportingUIErrorProcessor
                .getString("ERR_CODE_025"), null);
        systemExit(FAILURE);
    }

    IReportRunnable design = null;
    RenderOptionBase option = new RenderOptionBase();

    if ((format.toLowerCase() != "pdf") && (format.toLowerCase() != "html"))
        format = "pdf"; // format par défaut
    option.setOutputFormat(format.toLowerCase()); // Format du fichier de
    // sortie

    /* Fin initialisation */
    /* Préparation de l'édition */
    try {
        design = engine.openReportDesign(templateLoc);
    } catch (EngineException e) {
        Main.getLogger().log(
                "Warning : Fail to open report Design file : "
                        + e.getMessage());
        ReportingUIErrorWriter.process(ReportingUIErrorProcessor
                .getString("ERR_CODE_026"), null);
        systemExit(FAILURE);
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof NullPointerException) {
            Main.getLogger().log(
                    "Error: Reporting engine path is not correct.");

            // Destroy the engine object.
            if (engine != null) {
                engine.destroy();
                engine = null;
            }
            ReportingUIErrorWriter.process(ReportingUIErrorProcessor
                    .getString("ERR_CODE_027"), null);
            systemExit(FAILURE);
        }
    }

    try {
        /*
         * Changement de source inspiré de
         * http://jsourcery.com/api/sourceforge/openreports/2.1/org/efs/openreports/engine/BirtReportEngine.source.html
         */
        ReportDesignHandle reportDH = (ReportDesignHandle) design
                .getDesignHandle();

        List birtDataSources = reportDH.getAllDataSources();
        Iterator iterator = birtDataSources.iterator();

        while (iterator.hasNext()) {
            OdaDataSourceHandle dataSH = (OdaDataSourceHandle) iterator
                    .next();

            try {
                // Mantis 04034, 04037 : Update FILELIST property for the
                // data source with name "Source".
                if (dataSH.getProperty("name").equals("Source")) {
                    dataSH.setStringProperty("FILELIST", dataSource
                            .getAbsolutePath());
                }
            } catch (SemanticException e) {
                Main.getLogger().log(
                        "Warning : SemanticException in path change"
                                + e.getMessage());
                systemExit(FAILURE);
            }
        }

        //drop Grid rows containing information about Location Host and Location Components.
        if(!isSummaryReport){
            if(isDetailedLocInfoDisabled){          
                reportDH.getElementByID(1112).dropAndClear();  // delete and clear the location hostname attribute          
                reportDH.getElementByID(1116).dropAndClear(); // delete and clear the location components attribute
            }               
        }

        design.setDesignHandle(reportDH);
        /* Fin changement source */
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);
        /* Fin de la préparation */

        /* Création PDF */
        option.setOutputFileName(outputFile + format); // Nom du fichier de
        // sortie
        task.setRenderOption(option); // Chargement du fichier      

        if (!isSummaryReport) {
            if (isWithout_DescInConfigFile) {
                task.setParameterValue("withoutDesc", new Boolean(isWithout_DescInConfigFile).toString());
            }
            if (isWithMessInConfigFile) {
                task.setParameterValue("withMess", new Boolean(isWithMessInConfigFile).toString());
            }
        }else{
            task.setParameterValue("totalDuration", XMLReportParsing.totalDurationString.substring(1, XMLReportParsing.totalDurationString.length()-2));
        }

        /* Fin création PDF */
        try {
            // On crée le fichier où l’on va écrire. S’il existe déjà, on
            // écrit à la suite
            task.run(); // Déclenchement de l'édition
            // ferme le fichier qui log le run de BIRT
        } catch (EngineException e) {
            e.printStackTrace();
            Main.getLogger().log(
                    "Error in Report Generation : " + e.getMessage());
            ReportingUIErrorWriter.process(ReportingUIErrorProcessor
                    .getString("ERR_CODE_028"), null);
            systemExit(FAILURE);
        }
        task = null;
    } catch (Exception e) {
        e.printStackTrace();
        Main.getLogger().log(
                "Error in Report Generation : " + e.getMessage());
        ReportingUIErrorWriter.process(ReportingUIErrorProcessor
                .getString("ERR_CODE_028"), null);
        systemExit(FAILURE);
    }
    // Destroy the engine object.
    if (engine != null) {
        engine.destroy();
        engine = null;
    }

    // Chemin A = C:/birt-runtime-2_0_1/Report Engine. Dans mon cas, j'ai
    // copier/coller le répertoire Report Engine dans mon répertoire lib où
    // je rassemble toutes mes
}

I have pasted only relevant method code. Please help.

A: 

Find all empty try-blocks like

} catch (EngineException e) {}

and DO something with the caught exception. I suggest for now that you do

   } catch (EngineException e) {
      throw new RuntimeException("PDF error", e);
   }

so you can see the stack trace.

Thorbjørn Ravn Andersen
I have done it and still no success. Actually program is not throwing any exceptions.It is simply creating pdf file but with 0 contents.
Sudhir
Do you need to tell the engine to close the file explicitly? Do you ignore an error code returned by one of the engine calls?
Thorbjørn Ravn Andersen
Sorry, i don't understand. Do i need to tell the engine to close the file explicitly? if yes, how?Whatever methods i have used for example, task.run(), above return void.
Sudhir