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");
}
}
是否下載了PDF正確的文件大小? –