2016-03-11 137 views
0

我试图下载PDF文件。当用户想要下载文件时。 PDF文件正在下载成功,但即时无法打开该下载的文件。PDF文件下载在Vaadin

相同的代码与文本文件正常工作。

需要帮助。

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

是否下载了PDF正确的文件大小? –

回答

0

我知道这有点迟,但也许这个答案可能对其他人有用。其实你的代码是OK,并能生成PDF,但问题是,你应该之前myPdfData.close();

而且正好添加内容summaryReportTable后添加summaryReportTablemyPdfData,只是作为一个建议,最好是一起工作临时文件比实际文件。例如,您可以使用

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