项目场景:

需求描述:上传附件后,可实现在线预览,这里就会存在一个问题,很多附件的类型是没法在线预览的,点击就会下载。除pdf/jpg/jpeg等,于是技术方案定,将word/excel等类型的文件转成pdf,并且使用nginx代理附件路径,实现预览效果。


技术实现

提示:要考虑到,一个附件肯定会被多人点击预览,从表结构设计的角度,要多加一个preview_path(预览文件地址)字段,第一次生成pdf后,将链接持久化到数据库,之后只有该值不为空,查询直接返回即可。

aspose从maven仓库拉取不下来,需手动下载本地添加到项目中。
aspose-cells-8.5.2.jar
aspose-words-15.8.0-jdk16.jar

链接:https://pan.baidu.com/s/1YBAfBJF2mzPDaJqxjQ6jkg  密码:srco


pom.xml添加

注意:maven打包的时候,要加上这句,不然打包有问题

ok,堆代码吧

<?xml version="1.0" encoding="UTF-8"?>
<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

枚举类,支持的附件类型,按自己需求增减

package com.szls.enums;import lombok.AllArgsConstructor;
import lombok.Getter;/*** com.szls.enums** @author smallNorth_Lee* @date 2022/8/11*/
@Getter
@AllArgsConstructor
public enum FileTypeEnum {PDF(".pdf"),XLSX(".xlsx"),DOCX(".docx"),DOC(".doc");/*** 文件类型*/private final String fileType;
}
package com.szls.utils;import com.aspose.words.*;
import com.szls.enums.FileTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;/*** word转化成pdf,实现在线预览** @author smallNorth_Lee* @date 2022/8/10*/
@Component
@RequiredArgsConstructor
public class AsposeWordUtil {private final AsposeExcelUtil asposeExcelUtil;/*** 验证License 若不验证则转化出的pdf文档会有水印产生*/@SneakyThrowsprivate static void getLicense() {try (InputStream is = AsposeWordUtil.class.getClassLoader().getResourceAsStream("License.xml")) {License license = new License();license.setLicense(is);}}/*** word转pdf** @param filePath    原文件地址* @param pdfFilePath pdf 文件地址* @param fileType    文件类型*/public void fileToPdf(String filePath, String pdfFilePath, String fileType) {if (FileTypeEnum.XLSX.getFileType().equals(fileType)) {asposeExcelUtil.excelToPdf(filePath, pdfFilePath);return;}getLicense();File file = new File(pdfFilePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}FileOutputStream os = null;try {os = new FileOutputStream(file);Document doc = new Document(filePath);TableCollection tables = doc.getFirstSection().getBody().getTables();for (Table table : tables) {RowCollection rows = table.getRows();table.setAllowAutoFit(false);for (Row row : rows) {CellCollection cells = row.getCells();for (Cell cell : cells) {CellFormat cellFormat = cell.getCellFormat();cellFormat.setFitText(false);cellFormat.setWrapText(true);}}}if (FileTypeEnum.DOC.getFileType().equals(fileType) || FileTypeEnum.DOCX.getFileType().equals(fileType)) {doc.save(os, SaveFormat.PDF);} else {throw new RuntimeException("暂不支持该文件类型在线预览!");}} catch (Exception e) {e.printStackTrace();} finally {if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}}}
}

package com.szls.utils;import com.aspose.cells.*;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;/*** excel转化成pdf,实现在线预览** @author smallNorth_Lee* @date 2022/8/10*/
@Component
public class AsposeExcelUtil {/*** 验证License 若不验证则转化出的pdf文档会有水印产生*/@SneakyThrowsprivate static void getLicense() {try (InputStream is = AsposeExcelUtil.class.getClassLoader().getResourceAsStream("License.xml")) {License license = new License();license.setLicense(is);}}/*** excel转pdf** @param filePath    原文件地址* @param pdfFilePath pdf 文件地址*/public void excelToPdf(String filePath, String pdfFilePath) {getLicense();File file = new File(pdfFilePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}FileOutputStream os = null;try {Workbook workbook = new Workbook(filePath);WorksheetCollection worksheet = workbook.getWorksheets();Worksheet sheet = worksheet.get(0);sheet.getPageSetup().setPrintGridlines(false);os = new FileOutputStream(file);workbook.save(os, SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();} finally {if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}}}
}

注意⚠️:这里的每一个类,不要引错jar包,每个方法都是独立的类!!!!


存在问题及解决方案:

代码写完,开开心心部署到服务器,小手那么一点,文件转换没问题!!!,举国欢庆,关机跑路!!!!
突然前端同学:你这导出怎么乱码!!!!辣鸡
于是你开始沉默,堕落,自我拉扯…

hold on bro,因为linux 缺少字体,windows直接去找c:\windows\fonts
把这些文件上传到服务器上,新建一个文件夹

mkdir  /usr/share/fonts/winsudo mkfontscalesudo mkfontdirsudo fc-cache -fv -vsudo chmod 755  /usr/share/fonts/winfc-list :lang=zh

服务重启,重新执行方法,OK~~~~~~~

【aspose】 word/excel转pdf,实现在线预览文件功能相关推荐

  1. Java实现 word.excel等文档在线预览

    实现的原理: 通过第三方工具openoffice,将word,excel,pppt,txt等文件转换为pdf文件流 第一步下载Apache OpenOffice 安装包安装运行 地址:https:// ...

  2. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

  3. office在线预览 word在线预览 .net在线预览 文件在线浏览接口

    office在线预览 word在线预览 excel在线预览 文件浏览接口服务 支持移动端浏览,只要能使用浏览器上网都可以使用,不需要安装任何第三方工具. 1.word在线预览 excel在线预览,.n ...

  4. 引用pdf插件在线预览的问题

    引用pdf插件在线预览带中文名称文件报错解决 解决 由图可知插件pdf.js在解析带有中文的pdf文件时出现乱码问题 直接去改pdf.js比较麻烦且不好改,我们可以在预览pdf时候这么定义: Prin ...

  5. js下载文件和在线预览文件

    // 在线预览文件let file = url;let lastIndex=file.lastIndexOf(".");let suffix=file.substring(last ...

  6. 使用PageOffice实现文档(word,excel,pdf)在线预览编辑

    最近发现一款不错的插件的PageOffice,地址是:http://www.zhuozhengsoft.com/Technical/ 他可以实现word,excel.pdf在线预览以及在线编辑.虽然商 ...

  7. html怎么转换到百度,类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版...

    类似百度文库在线预览文档flash版(支持word.excel.ppt.pdf)+在线预览文档html版 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现 ...

  8. 【SpringBoot】40、SpringBoot中使用Aspose将文件转为PDF实现在线预览

    一.简介 Aspose 是 .NET 和 Java 开发组件以及为 Microsoft SQL Server Reporting Services 和 JasperReports 等平台提供渲染扩展的 ...

  9. 关于论坛实现pdf,word等文档在线预览的功能探讨

    论坛引擎:Discuz! 2.5 工具:flexpaper1.5.6 二次开发版本 本文主要探讨大体的思路及相关技术难点问题,属个人工作之余的一些小小开发,仅供探讨,嘿嘿... 实现效果如图: 论坛为 ...

最新文章

  1. 你想要的宏基因组-微生物组知识全在这
  2. 智能硬件这5大领域竞争升级,将迎发展新模式
  3. Android日志拦截器,Retrofit2日志拦截器的使用
  4. Nginx配置以及域名转发
  5. 优盘提示插入多卷集的最后一卷解决办法(5)
  6. java调mongodb自定义函数,自定义UDF函数,从hive保存到mongodb
  7. Atcoder Grand Contest 010 B - Boxes 差分
  8. 习题1.29 (积分方法的优化---simpson规则)
  9. javascript学习笔记(十九) 节点的操作
  10. XSS(跨站脚本攻击)漏洞解决方案
  11. 2022-2028全球骨科创伤植入物行业调研及趋势分析报告
  12. 函数计算机显示RAD,计算器rad是什么意思
  13. sdn交换机与普通交换机区别—Vecloud
  14. oracle无法进入nomount状态,数据库进入nomount状态
  15. 将压缩包变成图片的小技巧
  16. emui华为java2p_同属华为,却是两个相对独立的系统,Magic系统和EMUI区别在哪?...
  17. 扫码枪回车键条码_条码扫描枪怎么设置换行?
  18. 卡片消除游戏 java版(代码+讲解)
  19. GOOGLE地图基站定位-Google Mobile Maps API
  20. 成为一名合格的java工程师

热门文章

  1. XMind6和XMind7有何不同
  2. 2021杨雪洋高考成绩查询,十万火急!多省已开通2020高考成绩查询通道(附各省最新查询网址)...
  3. linux系统下替换图片,GIMP 图像处理软件如何更换图片背景色
  4. Qt下实现XML、INI、JSON的文件读写示例开发
  5. (转)图解Intel电脑组装过程
  6. 运动轨迹-GPS数据、经纬度转换
  7. 技术英语单词中英文对照
  8. 《前端架构:从入门到微前端》—— 带你成为前端架构师
  9. POI报表——模板打印 AND 海量数据导出
  10. 什么是IOC容器——简单明了