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;
C.安装字体;
D.
E.
F.
2. 访问地址:http://localhost:8012/
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}