-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hey all!
I'm running into a problem with exporting a jasperreport object as a PDF using MVVM.
The general idea is that I have a jasperreport, which works fine. But I also have a series of buttons to export said report in various formats. Here is a snippet from the .zul:
<vbox width="100%" align="right" id="buttonbar">
<hbox width="160px" height="40px" align="right">
<toolbarbutton onClick="@command('downloadPDF')"
id="pdfReport" image="/images
/pdf_ico.jpg"/>
</hbox>
</vbox>
<jasperreport src="@load(vm.reportSource)" type="@load(vm.reportType)"
parameters="@load(vm.parameters)" datasource="@load(vm.datasource)" width="100%" height="700px" />
Pretty straight forward, and the initial jasperreport looks great. But the export is what I'm having problems with.
@Command
public void downloadPDF(){
System.out.println("****downloadPDF****");
reportDownload = new Jasperreport();
reportDownload.setDatasource(datasource);
reportDownload.setParameters(parameters);
reportDownload.setSrc(Executions.getCurrent().getDesktop().getWebApp().getRealPath("/") + "statementTemplate.jasper");
reportDownload.setType("pdf");
Filedownload.save(reportDownload.getReport(), selectedStatement.getFullDate().getValue()+"."+reportDownload.getReport().getFormat());
}
This worked nicely when using MVC, so I figured it would work using MVVM. However, all I get are empty PDF documents.
Suggestions?
Edit: At sjoshi's suggestion, I tried populating with the jasperPrint object, my resulting code is as follows:
JRDataSource ds = datasource;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(Executions.getCurrent().getDesktop().getWebApp().getRealPath("/") + "statementTemplate.jasper", parameters, ds);
byte[] ba = JasperExportManager.exportReportToPdf(jasperPrint);
System.out.println("bytestream: " + ba.length);
Filedownload.save(ba, reportDownload.getReport().getContentType(), selectedStatement.getFullDate().getValue()+"." + reportDownload.getReport().getContentType());
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This gave me the same issue of blank documents, but on further inspection I'm seeing that the byte array is ending up nearly empty, even though the datasource has 33 records. The byte array should have a length of about 59k. So I think the source of the problem is somewhere with the application of the datasource to the report, a step missing or something.
Is there something that goes on in the background between a jasperreport object being populated with data and it being displayed in a <jasperreport> tag? I'm not doing that with this MVVM setup, but my guess would be that something is missing there.
try it may be work. this code working fine for me.
FileInputStream inputStream;
try {
File dosfile = new File(saveDosPath);
if (dosfile.exists()) {
inputStream = new FileInputStream(dosfile);
Filedownload.save(inputStream, new MimetypesFileTypeMap().getContentType(dosfile), dosfile.getName());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I looked into the interface JRDataSouce
public interface JRDataSource
{
public boolean next() throws JRException;
public Object getFieldValue(JRField jrField) throws JRException;
}
there is a cursor method next()
to control the row of datasource, and from your code , you reused the same datasource. so it might be already moved to last row then you got empty result.
you could try to use a new datasource (or reset it ?) , or why not get report from component jasperreport directly (if the argument are all the same)?
something like:
<vbox width="100%" align="right" id="buttonbar">
<hbox width="160px" height="40px" align="right">
<toolbarbutton onClick="@command('downloadPDF',report=reportComp.report)"
id="pdfReport" image="/images
/pdf_ico.jpg"/>
</hbox>
</vbox>
<jasperreport id="reportComp" src="@load(vm.reportSource)" type="@load(vm.reportType)"
parameters="@load(vm.parameters)" datasource="@load(vm.datasource)" width="100%" height="700px" />
@Command
public void downloadPDF(@BindParam("report") Media report){
Filedownload.save(report, selectedStatement.getFullDate().getValue()+"."+report.getFormat());
}
See this blog Integrate Dyanmic Jasper Reprot with ZK may be it will help you as there developer used MVVM .
Asked: 2013-02-18 23:34:04 +0800
Seen: 110 times
Last updated: Feb 20 '13
Databinding and auto-complete on combobox
Composite component and bind in ZK 6
How to detach / reattach MVVM windows?
Is there a way to resolve view model properties as input to client side javascripts?
How can I synchronize data in a ListBox in MVVM ? [closed]
MVVM Validator: class not found ? [closed]
How to Call Child ViewModel Method from Parent Window? [closed]
the code looks fine, and I am curious why you have NotifyChange of dlReportSource, reportType and reportDownload. and what are them? (especially reportDownload, looks like you binding it(a UI) to some where
dennis ( 2013-02-19 05:05:41 +0800 )editreportDownload is simply a report object I'm using in order to have something available to be downloaded. It is not bound to the UI.
The important part is how to get the Filedownload.save to download a report to a client AND have data in it. The download part is easy, but the report shows empty.
kryselmire ( 2013-02-19 16:56:47 +0800 )editSjoshi - Creating the report is not the issue. That part works fine. Its trying to create a downloadable version in various formats (.pdf, .xls, .csv, .doc) that is proving difficult. The report downloads, but it is empty.
Thank you for the link, its great to have additional resources!
kryselmire ( 2013-02-19 17:05:59 +0800 )editIf it is empty it mean somewhere you have issue in report generation so try the blog example in your machine and tell me if it is working or not
sjoshi ( 2013-02-19 18:00:24 +0800 )editso blog example yields the same results. However, I was fooling with it and it looks like the issue is that something is going wrong with the population of the bytestream. The datasource shows correctly with 33 records but the bytestream never gets beyond 1266 (should be 52000 or so).
kryselmire ( 2013-02-19 21:35:04 +0800 )edit