PDF文件下载在Vaadin

时间:2016-03-11 16:44:14

标签: vaadin vaadin7

我试图下载pdf文件。当用户想要下载文件时。 Pdf文件下载成功但我无法打开下载的文件。

相同的代码可以正常处理文本文件。

需要帮助。

// download button code
Button downloadBtn = CommonComponents.getUploadImageButton("Download Statement", "150px");
OnDemandStreamResource myResource = createOnDemandResource(summaryReportList);
OnDemandFileDownloader fileDownloader = new OnDemandFileDownloader(myResource);
fileDownloader.extend(downloadBtn);

// generating pdf here
private OnDemandStreamResource createOnDemandResource(ArrayList<SummaryReportSearchResult> summaryReportList)
{
    return new OnDemandStreamResource()
    {
        public ByteArrayInputStream getStream()
        {
            // this part defines the actual content which will be
            // returned to the browser everytime the user pushes
            // the download button
            Date currentDate = new Date();
            String fileName = "report_" + new SimpleDateFormat("ddmmyyyyhhmmss").format(currentDate);
            String filePath = new AppConstants().downloadFilePath;

            File downloadFile = new File(filePath + fileName + ".pdf");

            try
            {
                Document myPdfData = new Document();
                PdfWriter.getInstance(myPdfData, new FileOutputStream(downloadFile.getAbsolutePath()));
                myPdfData.open();

                PdfPTable summaryReportTable = new PdfPTable(4);
                PdfPCell tableCell;
                summaryReportTable.setWidthPercentage(99.0f);

                tableCell = new PdfPCell(new Phrase("DATE RANGE", new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                summaryReportTable.addCell(tableCell);

                tableCell = new PdfPCell(new Phrase("TRANSACTION TYPE", new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                summaryReportTable.addCell(tableCell);

                tableCell = new PdfPCell(new Phrase("COUNT", new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                summaryReportTable.addCell(tableCell);

                tableCell = new PdfPCell(new Phrase("COMMISSION", new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                summaryReportTable.addCell(tableCell);

                for (SummaryReportSearchResult summaryReportSearchResult : summaryReportList)
                {
                    tableCell = new PdfPCell(new Phrase(summaryReportSearchResult.getTransactionDate(), new Font(FontFamily.TIMES_ROMAN, 8)));
                    summaryReportTable.addCell(tableCell);

                    tableCell = new PdfPCell(new Phrase(summaryReportSearchResult.getTransactionType(), new Font(FontFamily.TIMES_ROMAN, 8)));
                    summaryReportTable.addCell(tableCell);

                    tableCell = new PdfPCell(new Phrase(summaryReportSearchResult.getCount(), new Font(FontFamily.TIMES_ROMAN, 8)));
                    summaryReportTable.addCell(tableCell);

                    tableCell = new PdfPCell(new Phrase(summaryReportSearchResult.getCommission(), new Font(FontFamily.TIMES_ROMAN, 8)));
                    summaryReportTable.addCell(tableCell);
                }
                myPdfData.close();
            }
            catch (Throwable e)
            {
                e.printStackTrace();
            }

            try
            {
                ByteArrayInputStream fileByteStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(downloadFile));
                downloadFile.delete();
                return fileByteStream;
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return null;
            }
        }

        public String getFilename()
        {
            Date currentDate = new Date();
            String fileName = "report_" + new SimpleDateFormat("ddmmyyyyhhmmss").format(currentDate);
            return fileName + ".pdf";
        }
    };
}


public class OnDemandFileDownloader extends FileDownloader
{
    /**
     * Provide both the {@link StreamSource} and the filename in an on-demand way.
     */
    public interface OnDemandStreamResource extends StreamSource
    {
        String getFilename();
    }

    private final OnDemandStreamResource onDemandStreamResource;

    public OnDemandFileDownloader(OnDemandStreamResource onDemandStreamResource)
    {
        super(new StreamResource(onDemandStreamResource, ""));
        this.onDemandStreamResource = onDemandStreamResource;
    }

    @Override
    public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException
    {
        getResource().setFilename(onDemandStreamResource.getFilename());
        return super.handleConnectorRequest(request, response, path);
    }

    private StreamResource getResource()
    {
        return (StreamResource) this.getResource("dl");
    }
}

1 个答案:

答案 0 :(得分:0)

我知道这有点晚了但也许这个答案可能对其他人有用。实际上您的代码是正常的并且能够生成PDF,但问题是您应该在summaryReportTable添加内容后myPdfData添加summaryReportTable,恰好在myPdfData.close();之前

此外,作为建议,最好使用临时文件而不是真实文件。例如,您可以使用

downloadFile = File.createTempFile("filename",".pdf");