File Download using MVVM

asked 2013-02-18 23:34:04 +0800

kryselmire gravatar image kryselmire
19 2

updated 2013-02-20 03:16:11 +0800

dennis gravatar image dennis
3669 1 6

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
<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.

 public void downloadPDF(){

    reportDownload = new Jasperreport();
    reportDownload.setSrc(Executions.getCurrent().getDesktop().getWebApp().getRealPath("/") + "statementTemplate.jasper");
    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.


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

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.

delete flag offensive retag edit


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 )edit

reportDownload 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 )edit

Sjoshi - 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 )edit

If 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 )edit

so 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

3 Answers

Sort by ยป oldest newest most voted

answered 2013-02-20 10:22:16 +0800

hswain gravatar image hswain flag of India
1763 3 10

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
link publish delete flag offensive edit

answered 2013-02-19 05:37:46 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8

See this blog Integrate Dyanmic Jasper Reprot with ZK may be it will help you as there developer used MVVM .

link publish delete flag offensive edit

answered 2013-02-20 03:24:27 +0800

dennis gravatar image dennis
3669 1 6

updated 2013-02-20 03:28:01 +0800

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
<jasperreport id="reportComp"  src="@load(vm.reportSource)" type="@load(vm.reportType)" 
parameters="@load(vm.parameters)" datasource="@load(vm.datasource)" width="100%" height="700px" />

public void downloadPDF(@BindParam("report") Media report){

   Filedownload.save(report, selectedStatement.getFullDate().getValue()+"."+report.getFormat());
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2013-02-18 23:34:04 +0800

Seen: 107 times

Last updated: Feb 20 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More