Download PDF with JasperReport

Hi,

I have problem in download a pdf file that generated by JasperReport. The problem is when download action done the result is blank output.
This is code I used:


public void buttonClick(ClickEvent event) {
    StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {                   
        public InputStream getStream() {
                            
            final InputStream reportInputStream = getClass().getClassLoader().getResourceAsStream("FileName.jasper");
            if (reportInputStream == null) {
                throw new RuntimeException("Could not find report source");
            }
            byte[] b = null;
            try {
                b = JasperRunManager.runReportToPdf(reportInputStream, null);
            }
            catch (JRException e) {
                e.printStackTrace();
             }
             return new ByteArrayInputStream(b);
        }
    };
                    
    StreamResource resource = new StreamResource(streamSource, "FileName.pdf", getApplication());
    resource.setMIMEType("application/pdf");
    resource.getStream().setParameter("Content-Disposition","attachment; filename=filename.pdf");
    getApplication().getMainWindow().open(resource, "_self");
}

What’s wrong with this code.
Vaadin version 6.7.0

Thanks.
/Shamsul

Problem solved. Got an idea from
here
.
This is my full code.


    @Override
    public void buttonClick(ClickEvent event) {
        FileOutputStream fos = null;
        File tempFile = null;
        try {
            tempFile = File.createTempFile("tmp", ".pdf");
            fos = new FileOutputStream(tempFile);
                
            String jrxml = "FileName.jrxml";
            String jrxmlPath = getClass().getClassLoader().getResource(".").getPath() + jrxml;
              
            String jasperCompile= JasperCompileManager.compileReportToFile(jrxmlPath);
            JasperPrint print = JasperFillManager.fillReport(jasperCompile, null, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, fos);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (JRException e) {
            e.printStackTrace();
        }
        finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String downloadFileName = System.currentTimeMillis() + "_filename.pdf";
        String contentType = "application/pdf";
        try {
            TemporaryFileDownloadResource resource = new TemporaryFileDownloadResource(getApplication(), downloadFileName, contentType, tempFile);
            getWindow().open(resource, "_self");
        }
        catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
        }
    }

JasperReport version 4.1.1.

Thanks.

/Shamsul