利用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,因为还有请求在跑。

 

转载于:https://www.cnblogs.com/time-on/p/7126359.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值