在 Vaadin 下载 PDF 文件 [英] PDF File download In Vaadin

查看:20
本文介绍了在 Vaadin 下载 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载 pdf 文件.当用户想要下载文件时.Pdf 文件正在成功下载,但我无法打开该下载的文件.

Im trying to download pdf file. when user want to download the file. Pdf file is downloading successfully but im not able to open that downloaded file.

相同的代码在文本文件上运行良好.

same code is working fine with text files.

需要帮助.

// 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");
    }
}

推荐答案

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

I know that it is a bit late but maybe this answer could be useful for the others. Actually your code is OK and is able to generate a PDF, but the problem is you should add summaryReportTable to the myPdfData after adding the content to summaryReportTable, exactly before myPdfData.close();

此外,作为一个建议,使用临时文件比使用真实文件更好.例如,您可以使用

Moreover just as an advice, it is better to work with temporary files than real files. For example you can use

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

这篇关于在 Vaadin 下载 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆