@Overridepublic void transform(InputStream in, OutputStream out, String fileName) throwsIOException {
UUID uuid=UUID.randomUUID();this.cacheFileName = fileName.substring(0, fileName.lastIndexOf(".")) + "-" +uuid;//设置本地缓存的路径
String tmpPath = localHtmlCachePath + IOUtils.DIR_SEPARATOR + this.cacheFileName + ".pdf";
String lowerFileName=fileName.toLowerCase();try{if (!newFile(tmpPath).exists()) {if (lowerFileName.endsWith(".xls") || lowerFileName.endsWith(".xlsx")|| lowerFileName.endsWith(".csv")) {
com.aspose.cells.Workbook workbook= newcom.aspose.cells.Workbook(in);
workbook.save(tmpPath, com.aspose.cells.SaveFormat.PDF);
}else if (lowerFileName.endsWith(".doc") || lowerFileName.endsWith(".docx")|| lowerFileName.endsWith(".rtf")) {
com.aspose.words.Document doc= newcom.aspose.words.Document(in);
doc.save(tmpPath, com.aspose.words.SaveFormat.PDF);
}else if (lowerFileName.endsWith(".ppt") || lowerFileName.endsWith(".pptx")|| lowerFileName.endsWith(".pps") || lowerFileName.endsWith(".ppsx")) {
com.aspose.slides.Presentation ppt= newcom.aspose.slides.Presentation(in);
ppt.save(tmpPath, com.aspose.slides.SaveFormat.Pdf);
}else if (lowerFileName.endsWith(".vdx") || lowerFileName.endsWith(".vsx")|| lowerFileName.endsWith(".vtx") || lowerFileName.endsWith(".vsd")|| lowerFileName.endsWith(".vsdx")) {
com.aspose.diagram.Diagram visio= newcom.aspose.diagram.Diagram(in);
visio.save(tmpPath, com.aspose.diagram.SaveFileFormat.PDF);
}else if (lowerFileName.endsWith(".pdf")) {
com.aspose.pdf.Document pdf= newcom.aspose.pdf.Document(in);
pdf.save(tmpPath, com.aspose.pdf.SaveFormat.Pdf);
}
}if (out instanceofResponseOutputStream) {
HttpServletResponse response=((ResponseOutputStream) out).getResponse();
response.sendRedirect("/filePreview/officeHtmlFileViwer/" + this.cacheFileName + ".pdf");
}
}catch(Exception ex) {throw newRuntimeException(ex);
}
}
本文介绍了一种将多种格式的办公文件转换为PDF的方法。利用Aspose库实现Excel、Word、PPT及Visio等文件到PDF的转换,并将其缓存到本地。支持.xls,.xlsx,.csv,.doc,.docx,.rtf,.ppt,.pptx,.pps,.ppsx,.vdx,.vsx,.vtx,.vsd,.vsdx和.pdf等文件格式。
2879

被折叠的 条评论
为什么被折叠?



