PDF File download In Vaadin

帅比萌擦擦* 提交于 2021-02-08 06:48:52

问题


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.

Help Required.

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


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


来源:https://stackoverflow.com/questions/35945563/pdf-file-download-in-vaadin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!