Hi to all!
I'm trying to run BIRT (in a Webapp) so to learn how to use it I'm running a test class in Eclipse with a main method to produce the report.
My strategy for the test (I'm new to BIRT) is to use to follow the ScriptedDataSource design example so I have a couple of classes to supply the Data Sources for the reportDesign ( which has 2 tables.. like 2 subreports I hope). If I run and view the report PDF using the IDE (Ecipse).. it work !
How can I run and view the report using my main method programmatically (as if by the WebApp)?
Am I using the right approach? Do you/how can you use a scriptedDatasource ? Maybe you don't use a ScripteddataSource to use BIRT in a WebApp ?
Please give any feedback you can, it will help ?
Thank you in advance !
it crashes at task.run();
I will attach my classes and report design ...
Below is the Error and reportGenerator.java
SEVERE: The output format pdf is not supported.
org.eclipse.birt.report.engine.api.UnsupportedFormatException: The output format pdf is not supported.
at org.eclipse.birt.report.engine.api.impl.EngineTask.setupRenderOption(EngineTask.java:1938)
at org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.doRun(RunAndRenderTask.java:96)
at org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.run(RunAndRenderTask.java:77)
at report.ReportGenerator.generatePDF(ReportGenerator.java:174)
at report.ReportGenerator.buildBills(ReportGenerator.java:144)
at report.ReportGenerator.main(ReportGenerator.java:93)
package report;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
/**
*
@Author jokrasa
*
@Desc: java module to create a single PDF
*/
public final class ReportGenerator {
private static Logger logger = Logger.getLogger("ReportGeneratorLog");//used in main
// instance Constants
private final String rptdesign = "patientScriptDS.rptdesign";
private final String separator = System.getProperty("file.separator");
private String workdir = "C:/work/projects/medrec/report-test";
//
these constants are relative to workdir, unlikely to be changed
private String engine_home; //= workdir + "/birt-runtime-2_3_2/ReportEngine";// Location of the BIRT plug-ins
private String designDirectory;// = workdir + "/BillsGenerator/designs";// Location that contains the reports design
private String outputDirectory;// = workdir + "/Bills";// Location that receive the bills
private String tempFilesDirectory;// = workdir + "/temp";// Location where to dispose temporary files generated by the application
//class variables, used in main
private static FileHandler fh;
//instance variables
private IReportEngine engine = null;// Report engine used to generate reports from design, thread safe
DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmssZ");
Date date = new Date();
private String timeStamp = dateFormat.format(date)+"";
private EngineConfig config = null;// Engine configuration
private IReportRunnable design = null;//runnable instance of BIRT report design
private String identity = null;
//__________________________________________________________________
/**
* Init pdf bills generator and start the pdf generating process.
*/
public static void main(final String args[])
{
//determine the location of the BillGenerator folder in relative to ReportGenerator.class
URL location;
String classLocation = ReportGenerator.class.getName().replace('.', '/') + ".class";
ClassLoader loader = ReportGenerator.class.getClassLoader();
if (loader == null){
System.err.println("Cannot load ReportGenerator");
System.exit(-1);
}
else {
location = loader.getResource(classLocation);
File log = new File(location.getPath());
String logLocation = log.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParent();
logLocation = logLocation+"/ReportGeneratorLog.log";
//init logging facility
try
{
fh = new FileHandler(logLocation,true);
logger.addHandler(fh);
logger.setLevel(Level.WARNING);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
}catch(IOException e){e.printStackTrace();
}catch(SecurityException e){e.printStackTrace();}
final ReportGenerator g = new ReportGenerator(args);
g.buildBills();
g.close();
}
}
//__________________________________________________________________
/**
* Init pdf bills generator and start the pdf generating process.
*
@param workdir
* absolute path of the workdir
*/
private ReportGenerator(final String args[]) {
//parse and assign params to instance variables
try {
identity = "testReport";
engine_home = "C:/birt-runtime-3_7_1/ReportEngine";// Location of the BIRT plug-ins
designDirectory = workdir + "/src/main/resources/";// Location that contains the report designs
outputDirectory = workdir + "/report";// Where the final PDF bills will be placed
tempFilesDirectory = workdir + "/temp";// Location where to dispose temporary files generated by the application
config = new EngineConfig();
config.setEngineHome(engine_home);
config.setLogConfig(tempFilesDirectory, Level.SEVERE);
config.setTempDir(tempFilesDirectory);
Platform.startup(config);
final IReportEngineFactory factory = (IReportEngineFactory)Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
}catch (Exception e){
exitWithErrors("Error when settings the BIRT Engine Config. \n\n", e);
}
}
//__________________________________________________________________
private void buildBills(){
// open the design file
final StringBuilder designName = new StringBuilder(designDirectory);
designName.append(separator);
//designName.append(billType);
designName.append(rptdesign);
try{
design = engine.openReportDesign(designName.toString());
}catch (final EngineException e){
exitWithErrors("Design " + designName + " was not found or is invalid! \n\n", e);
}
// parameters from the report design
IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask(design);
final HashMap<?, ?> paramvalues = task.getParameterValues();
generatePDF(identity, paramvalues);
}
/**
* Generate the pdf file for the bill with the specifed bill id.
*
@param billId
* the identifier of the group or the individual for which the bill is built.
*
@param params
* the run parameters needed by the Report Engine to generate the bill.
*/
private void generatePDF(final String billId, final Map<?, ?> params) {
try {
String barcode="";
final IRunAndRenderTask task = engine.createRunAndRenderTask(design);
final StringBuffer output = new StringBuffer(outputDirectory);
output.append(separator);
output.append(timeStamp);
output.append(separator);
output.append(separator);
barcode = barcode+timeStamp;
output.append(barcode);
output.append(".pdf");
//task.setParameterValues(params);
// Render the bill
final PDFRenderOption options = new PDFRenderOption();
options.setOutputFileName(output.toString());
options.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF); //.OUTPUT_FORMAT_PDF);
task.setRenderOption(options);
task.run();
task.close();
//storePDF(output.toString(), barcode);
}
catch (Exception e) {
logger.log(Level.SEVERE, "Cannot successfully render the bill for :" + billId);
logger.log(Level.SEVERE, e.getMessage());
e.printStackTrace();
System.err.println("Cannot successfully render the bill for :" + billId);
}
}
/**
* Destroy the BIRT report engine.
*/
private void close() {
try {
engine.destroy();
Platform.shutdown();
}catch (final Exception e){
logger.log(Level.SEVERE, "Errors occurs when shutting down the Platform! \n");
}
}
/**
* Display error messages
*
*
@param err
*/
private static void exitWithErrors(final String err, final Throwable e) {
logger.log(Level.SEVERE, err);
System.err.append(err);
if (e != null) {
e.printStackTrace(System.err);
logger.log(Level.SEVERE, e.getStackTrace().toString());
}
Platform.shutdown();
System.exit(-1);
}
}