Home
Analytics
save birt reports in different formats based upon user selection
android272
<p>I have set up my application such that a user chooses their desired file format they would like to revise a report. I finally got the report generated and formatted good enough for now. But I still have it hard coded to save as a ".rptdesign". This worked fine up till now since I could just view this as pdf or odt in eclipse. But now I would like to let my code change what file format to save in.</p>
<pre class="_prettyXprint _lang-">
designHandle.saveAs("path/to/report/sample.rptdesign");
designHandle.close();
</pre>
<p>above is how my code saves the report now. I tried changing .rptdesign to .pdf and .doc but nether worked. .pdf created a file that could not open and .doc was a file of xml that can barely be read.</p>
<p> </p>
<p>when I select these formats in the view report dropdown it looks fine.</p>
<p> </p>
<p>dose anyone know how to save my reports as different file formats?</p>
Find more posts tagged with
Comments
JFreeman
<p>You can use the Report Engine API (REAPI) to render the report to different output formats such as PDF.</p>
<p> </p>
<p>Take a look at the REAPI documentation here: <a data-ipb='nomediaparse' href='
https://eclipse.org/birt/documentation/integrating/reapi.php'>https://eclipse.org/birt/documentation/integrating/reapi.php</a></p>
;
android272
<p>So I found this here: <a data-ipb='nomediaparse' href='
http://developer.actuate.com/deployment-center/integrating-birt-into-applications/birt-with-html/'>http://developer.actuate.com/deployment-center/integrating-birt-into-applications/birt-with-html/</a>.</p>
;
<pre class="_prettyXprint">
private String saveReport(String format) throws EngineException {
IReportEngine engine = null;
EngineConfig config = null;
try {
// start up Platform
config = new EngineConfig();
config.setLogConfig("/logs", java.util.logging.Level.FINEST);
Platform.startup(config);
// create new Report Engine
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
// open the report design
IReportRunnable design = null;
design = engine.openReportDesign("D:/main-workspace/ServletDemo/reports/sample.rptdesign");
// create RunandRender Task
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
// pass necessary parameters
task.setParameterValue("myParam", format);
task.validateParameters();
ByteArrayOutputStream outs = new ByteArrayOutputStream();
...</pre>
<p>So this next section is where my sets the render options of my report. When the format is xls(excel) it saves it to my computer. However when I go to open it, it tells me that the file format and the extension of 'report.xls' don't match. dose anyone know why this is?</p>
<pre class="_prettyXprint _lang-">
...
// set render options including output type
if (format.equals("html")) {
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputStream(outs);
options.setImageHandler(new HTMLServerImageHandler());
options.setBaseImageURL("images");
options.setImageDirectory("D:/main-workspace/ServletDemo/WebContent/assets/images");
options.setEmbeddable(true);
options.setOutputFormat(format);
task.setRenderOption(options);
}
if (format.equals("xls")) {
EXCELRenderOption options = new EXCELRenderOption();
options.setOutputFormat(format);
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/report.xls");
task.setRenderOption(options);
}
...</pre>
<pre class="_prettyXprint _lang-">
...
// run task
String output;
task.run();
output = outs.toString();
task.close();
engine.destroy();
return output;
} catch (Exception ex) {
ex.printStackTrace();
return "Error";
} finally {
Platform.shutdown();
RegistryProviderFactory.releaseDefault();
}
}
</pre>
<p>Note that there is nothing between the ...ellipsis...</p>
android272
<p>Am I setting these render options right? no matter what I do when I save reports in .odt, .ors, .pdf, and .doc they only show the header row and no content. .xls says that the file format and the extension do not match that it may be corrupt.</p>
<pre class="_prettyXprint _lang-">
if (format.equals("ods")) {
RenderOption options = new RenderOption();
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/" + fileName + "." + format);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
// options.setOption(PDFRenderOption.PAGE_OVERFLOW,
options.setOption(PDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
options.setOption(IRenderOption.HTML_PAGINATION, Boolean.FALSE);
options.setOutputFormat(format);
task.setRenderOption(options);
}
if (format.equals("odt")) {
PDFRenderOption options = new PDFRenderOption();
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/" + fileName + "." + format);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
// options.setOption(PDFRenderOption.PAGE_OVERFLOW,
options.setOption(PDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
options.setOption(IRenderOption.HTML_PAGINATION, Boolean.FALSE);
options.setOutputFormat(format);
task.setRenderOption(options);
}
if (format.equals("pdf")) {
PDFRenderOption options = new PDFRenderOption();
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/" + fileName + "." + format);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
// options.setOption(PDFRenderOption.PAGE_OVERFLOW, options.setOption(PDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
options.setOption(IRenderOption.HTML_PAGINATION, Boolean.FALSE); options.setOutputFormat(format);
task.setRenderOption(options);
}
if (format.equals("xls")) {
EXCELRenderOption options = new EXCELRenderOption();
options.setOutputFormat(format);
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/" + fileName + "." + format);
task.setRenderOption(options);
}
if (format.equals("doc")) {
RenderOption options = new RenderOption();
options.setOutputFormat(format);
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/" + fileName + "." + format);
task.setRenderOption(options);
}
if (format.equals("html")) {
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFormat(format);
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/" + fileName + "." + format);
task.setRenderOption(options);
}
</pre>
JFreeman
<p>Can you attach a full class with the trimmed down version of your code that reproduces your issue we can run?<br>
For instance, a simplified version that only exports to the XLS with your setup.</p>
<p> </p>
<p>I'm not able to replicate your issue currently and with this I may be able to reproduce your issue to get further.</p>
android272
<p>I finally got my code to save in the various formats, but when you open them they only show the header row and no data in the detail rows. Some file formats have issues such as xls it always has problems with its mime type and file format not matching.</p>
<pre class="_prettyXprint _lang-">
ReportDesignHandle designHandle = null;
ElementFactory designFactory = null;
StructureFactory structFactory = null;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
handleRequest(request, response);
} catch (StyleSheetException e) {
e.printStackTrace();
} catch (EngineException e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
handleRequest(request, response);
} catch (StyleSheetException e) {
e.printStackTrace();
} catch (EngineException e) {
e.printStackTrace();
}
}
// Gets called by my doGet and doPost
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, StyleSheetException, EngineException, ServletException {
// get report name and launch the engine
String format = request.getParameter("format");
String sql = request.getParameter("sql"); String json = request.getParameter("json");
// runReport is a method that constructs my report and saves a report.rptdesign
//this.runReport(sql, json, format);
String mimeType = null;
if (format.equals("ods")) {
response.setContentType("application/vnd.oasis.opendocument.spreadsheet");
mimeType = "application/vnd.oasis.opendocument.spreadsheet";
}
if (format.equals("odt")) {
response.setContentType("application/vnd.oasis.opendocument.text");
mimeType = "application/vnd.oasis.opendocument.text";
}
if (format.equals("pdf")) {
response.setContentType("application/pdf");
mimeType = "application/pdf";
}
if (format.equals("xls")) {
response.setContentType("application/vnd.ms-excel");
mimeType = "application/vnd.ms-excel";
}
if (format.equals("doc")) {
response.setContentType("application/msword");
mimeType = "application/msword";
}
if (format.equals("html")) {
response.setContentType("text/html");
mimeType = "text/html";
}
// gets MIME type of the file
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
// saveReport() is shown below. This is the method that saves the report.rptdesign to a different format
this.saveReport(format, request, response);
// reads input file from an absolute path
String filePath = "path/to/report/report." + format;
File downloadFile = new File(filePath);
FileInputStream inStream = new FileInputStream(downloadFile);
// obtains ServletContext
ServletContext context = getServletContext();
// modifies response response.setContentLength((int) downloadFile.length());
// forces download String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
response.setHeader(headerKey, headerValue);
// obtains response's output stream OutputStream stream = response.getOutputStream();
byte[] buffer = new byte[4096];
int readBytes = 0;
while ((readBytes = inStream.read(buffer)) != -1) {
stream.write(buffer, 0, readBytes);
}
inStream.close();
stream.close();
}
</pre>
<div>
<p> </p>
</div>
<p>This method is what takes my report.rptdesign and saves it to the disk as the various file formats. You will need your own report.rptdesign as I don't know how to upload a test one on this site.</p>
<pre class="_prettyXprint _lang-">
private void saveReport(String format, HttpServletRequest request, HttpServletResponse response) throws EngineException {
IReportEngine engine = null;
EngineConfig config = null;
try {
// start up Platform
config = new EngineConfig();
config.setLogConfig("/logs", java.util.logging.Level.FINEST);
Platform.startup(config);
// create new Report Engine
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
// open the report design
IReportRunnable design = engine.openReportDesign("path/to/reports/report.rptdesign");
// create RunandRender Task
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
// Set parent classloader for engine
task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, ServletDemo.class.getClassLoader());
// pass necessary parameters
task.setParameterValue("format", format);
task.validateParameters();
ByteArrayOutputStream outs = new ByteArrayOutputStream();
// set render options including output type
if (format.equals("ods")) {
RenderOption options = new RenderOption();
// options.setOutputStream(response.getOutputStream());
options.setOutputFileName("path/to/reports/report." + format);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
// options.setOption(PDFRenderOption.PAGE_OVERFLOW,
options.setOption(PDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
options.setOption(IRenderOption.HTML_PAGINATION, Boolean.FALSE);
options.setOutputFormat(format);
task.setRenderOption(options);
}
if (format.equalsIgnoreCase("odt")) {
RenderOption options = new RenderOption();
// options.setOutputStream(response.getOutputStream());
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/report." + format);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
// options.setOption(PDFRenderOption.PAGE_OVERFLOW,
options.setOption(PDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
options.setOption(IRenderOption.HTML_PAGINATION, Boolean.FALSE);
options.setOutputFormat(format);
task.setRenderOption(options);
}
if (format.equalsIgnoreCase("pdf")) {
PDFRenderOption options = new PDFRenderOption();
// options.setOutputStream(response.getOutputStream());
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/report." + format);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
// options.setOption(PDFRenderOption.PAGE_OVERFLOW,
options.setOption(PDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
options.setOption(IRenderOption.HTML_PAGINATION, Boolean.FALSE);
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
task.setRenderOption(options);
}
if (format.equalsIgnoreCase("xls")) {
EXCELRenderOption options = new EXCELRenderOption();
options.setOutputFormat(format);
// options.setOutputStream(response.getOutputStream());
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/report." + format);
task.setRenderOption(options);
}
if (format.equalsIgnoreCase("doc")) {
RenderOption options = new RenderOption();
options.setOutputFormat(format);
// options.setOutputStream(response.getOutputStream());
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/report." + format);
task.setRenderOption(options);
}
if (format.equalsIgnoreCase("html")) {
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFileName("D:/main-workspace/ServletDemo/reports/report." + format);
options.setOutputFormat(format);
// options.setOutputStream(response.getOutputStream());
options.setEmbeddable(false);
task.setRenderOption(options);
}
// run task
task.run();
task.close();
engine.destroy();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Platform.shutdown();
RegistryProviderFactory.releaseDefault();
}
}
</pre>
android272
<p>I was also working on see if this code would work instead for my saveReport() method. It is code from the link you posted a few days ago. But This is even worse than the current version of saveReport() as it saves files but it has absolutely no content saved to it not even the header.</p>
<pre class="_prettyXprint _lang-">
IReportEngine engine = null;
EngineConfig config = null;
config = new EngineConfig();
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
// Open a report design
IReportRunnable design = engine.openReportDesign("D:/main-workspace/ServletDemo/reports/report.rptdesign");
// Create task to run the report - use the task to execute the report
// and save to disk.
IRunTask runTask = engine.createRunTask(design);
// Set parent classloader for engine
runTask.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, ServletDemo.class.getClassLoader());
// run the report and destroy the engine
runTask.run("D:/main-workspace/ServletDemo//report.rptdesign");
runTask.close();
// Open a report document
IReportDocument iReportDocument = engine.openReportDocument("c:/work/test/Pages.rptdocument");
// Create Render Task
IRenderTask rinderTask = engine.createRenderTask(iReportDocument);
// Set parent classloader report engine
runTask.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, ServletDemo.class.getClassLoader());
IRenderOption options = new RenderOption();
options.setOutputFormat("html");
options.setOutputFileName("output/resample/eventorder.html");
if (options.getOutputFormat().equalsIgnoreCase("html")) {
HTMLRenderOption htmlOptions = new HTMLRenderOption(options);
htmlOptions.setImageDirectory("output/image");
htmlOptions.setHtmlPagination(false);
// set this if you want your image source url to be altered
// If using the setBaseImageURL, make sure
// to set image handler to HTMLServerImageHandler
htmlOptions.setBaseImageURL("http://myhost/prependme?image=");
htmlOptions.setHtmlRtLFlag(false);
htmlOptions.setEmbeddable(false);
} else if (options.getOutputFormat().equalsIgnoreCase("pdf")) {
PDFRenderOption pdfOptions = new PDFRenderOption(options);
pdfOptions.setOption(IPDFRenderOption.FIT_TO_PAGE, new Boolean(true));
pdfOptions.setOption(IPDFRenderOption.PAGEBREAK_PAGINATION_ONLY, new Boolean(true));
}
// file based images
// options.setImageHandler(new HTMLCompleteImageHandler())
// Web based images. Allows setBaseImageURL to prepend to img src tag
options.setImageHandler(new HTMLServerImageHandler());
rinderTask.setRenderOption(options);
rinderTask.setPageRange("1-2");
rinderTask.render();
iReportDocument.close();
</pre>
android272
<p>So I figured out that nothing is wrong with my code. The problem was that the jdbc driver was not set properly... Now to figure out this dynamic formatting.</p>
android272
<p>Although It seems as I still have a problem with saving xls files.</p>
JFreeman
<p>That is interesting the issue seems to have been due to setting your JDBC driver.</p>
<p> </p>
<p>What is the behavior you are seeing with the XLS files now that you have resolved the JDBC issue and the other output formats are working?</p>