Revision history [back]

click to hide/show revision 1
initial version

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

kryselmire gravatar image kryselmire

File Download using MVVM

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
 @NotifyChange({"dlReportSource", "reportType", "reportDownload"})
 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?

File Download using MVVM

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
 @NotifyChange({"dlReportSource", "reportType", "reportDownload"})
"reportType"})
 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?

File Download using MVVM

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
 @NotifyChange({"dlReportSource", "reportType"})
 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?

File Download using MVVM

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?

File Download using MVVM

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.

File Download using MVVM

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

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