SpringBoot 整合文件在线预览(kkFileView)

简介:kkFileView支持doc,docx,Excel,pdf,txt,zip,rar,图片等文件在线预览。

1. 安装

  A. 安装libreOffice插件,从官网下载LibreOffice_7.1.0.2_Linux_x86-64_rpm.tar.gz;

  B. 查看版本:cd /opt/libreoffice7.1/program && ./soffice --version;

image

  C.安装字体;

  D.

  E.

  F.

 

2. 访问地址:http://localhost:8012/ 

image

 3. 开发word转pdf接口

@PostMapping("/wordToPdf")
    public byte[] wordToPdf(@RequestBody byte[] sourceBytes) {
        if (Objects.isNull(sourceBytes) || sourceBytes.length == 0) {
            return null;
        }

        String fileName = String.format("%s/%s.docx", ConfigConstants.getFileDir(), UUID.randomUUID());
        File file = new File(fileName);
        String covertFileName = fileName.replace(".docx", ".pdf");
        File convertFile = null;
        try {
            FileUtils.writeByteArrayToFile(file, sourceBytes);
            FileAttribute fileAttribute = new FileAttribute();
            OfficeToPdfService.converterFile(file, covertFileName, fileAttribute);

            convertFile = new File(covertFileName);
            return FileUtils.readFileToByteArray(convertFile);
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        } finally {
            // 完成后,删除文件
            file.delete();
            if (Objects.nonNull(convertFile)) {
                convertFile.delete();
            }
        }
    }

4. application.properties配置

office.home = ${KK_OFFICE_HOME:default}
base.url = ${KK_BASE_URL:default}

 

可参考:kkFileView在线文档预览系统

posted @ 2020-07-08 12:53  如幻行云  阅读(3232)  评论(0)    收藏  举报