zoukankan      html  css  js  c++  java
  • 利用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,因为还有请求在跑。

  • 相关阅读:
    如何下载Bilibili视频
    pyqt5 主界面打开新主界面、打开Dialog、打开提示框的实现模板
    【爬坑】python3+pyqt5+pyinstaller 打包成exe的各种问题
    【PyQt5-Qt Designer】QComboBox(下拉列表框) 使用模板
    【PyQt5-Qt Designer】读取txt文件在打印
    pyqt5核心-信号与槽(第二弹)
    使用QT设计师-信号和槽signal-slot(第一弹)
    【python基础】python程序打包成.exe运行时会弹出黑框
    【pyqt5】QdateTimeEdit(日期时间)
    pyqt5-对文本样式进行操作
  • 原文地址:https://www.cnblogs.com/time-on/p/7126359.html
Copyright © 2011-2022 走看看