利用Aspose将office转换成HTML、PDF格式


一、重要转换代码:

@Override public void transform(InputStream in, OutputStream out, String fileName) throws IOException { 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 (!new File(tmpPath).exists()) { if (lowerFileName.endsWith(".xls") || lowerFileName.endsWith(".xlsx") || lowerFileName.endsWith(".csv")) { com.aspose.cells.Workbook workbook = new com.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 = new com.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 = new com.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 = new com.aspose.diagram.Diagram(in); visio.save(tmpPath, com.aspose.diagram.SaveFileFormat.PDF); } else if (lowerFileName.endsWith(".pdf")) { com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(in); pdf.save(tmpPath, com.aspose.pdf.SaveFormat.Pdf); } } if (out instanceof ResponseOutputStream) { HttpServletResponse response = ((ResponseOutputStream) out).getResponse(); response.sendRedirect("/filePreview/officeHtmlFileViwer/" + this.cacheFileName + ".pdf"); } } catch (Exception ex) { throw new RuntimeException(ex); } }

 

二、创建了一个maven 项目,用到的相关jar如下:

三、遇到问题:

1、缓存文件夹不存在则创建:

// 文件夹不存在则创建

if (!new File(localHtmlCachePath).exists()) { new File(localHtmlCachePath).mkdirs(); }

 

2、有水印:获取lic有问题:不能图省事,要以此次的获取InputStream(setLicense方法执行完可能是把InputStream给close了)

 

 3、response.sendRedirect("/preview/officeHtmlFileViwer/" + this.cacheFileName + ".html");执行之后没有打开新页面

原因:不能close,因为还有请求在跑。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM