实现思路:
将cad(dwg、dxf、dwf)、word(doc、docx)、excle(xls,xlsx)、ppt(ppt,pptx)等类型的文件转为pdf
然后前端通过pdfjs实现预览

一、引入依赖

<!-- 导入本地jar -->
<!-- 操作ppt -->
<dependency><groupId>com.aspose</groupId><artifactId>aspose-slides</artifactId><version>1.0</version><scope>system</scope><systemPath>${project.basedir}/lib/aspose.slides-15.9.0.jar</systemPath>
</dependency>
<!-- 操作excel -->
<dependency><groupId>com.aspose</groupId><artifactId>aspose-cells</artifactId><version>1.0</version><scope>system</scope><systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<!-- 操作word-->
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>1.0</version><scope>system</scope><systemPath>${project.basedir}/lib/aspose-words-18.6-jdk16.jar</systemPath>
</dependency>
<!-- 操作cad-->
<dependency><groupId>com.aspose</groupId><artifactId>aspose-cad</artifactId><version>1.0</version><scope>system</scope><systemPath>${project.basedir}/lib/aspose-cad-19.9.jar</systemPath>
</dependency>
<!-- 操作文本文件-->
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency>

二、编写工具类

package com.logan.base.util;
import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.core.io.ClassPathResource;
import java.io.*;/**
* @author:logan
* @ClassName: PdfUtil
* @date: 2021/6/3  17:27
* @Description: TODO
*/
public class PdfUtil {private static final String myLicense = "<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>";public static void othersToPdf(String extension,InputStream in,OutputStream out) {if ("doc".equalsIgnoreCase(extension) || "docx".equalsIgnoreCase(extension)) {wordToPdf(in,out);}else if ("xls".equalsIgnoreCase(extension) || "xlsx".equalsIgnoreCase(extension)) {excelToPdf(in,out);}else if ("ppt".equalsIgnoreCase(extension) || "pptx".equalsIgnoreCase(extension)) {pptToPdf(in,out);}else if ("txt".equalsIgnoreCase(extension)) {txtToPdf(in,out);}else if ("dwg".equalsIgnoreCase(extension)||"dwf".equalsIgnoreCase(extension)||"dxf".equalsIgnoreCase(extension)) {cadToPdf(in,out);}}/*** word转pdf* @param inputStream* @param outputStream*/public static void wordToPdf(InputStream inputStream, OutputStream outputStream) {ByteArrayInputStream licenseInputStream = null;try{//设置证书licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());License wordLicense = new License();wordLicense.setLicense(licenseInputStream);//转化Document document = new Document(inputStream);document.save(outputStream, SaveFormat.PDF);licenseInputStream.close();}catch (Exception e){e.printStackTrace();System.out.println("word转PDF失败");}}/*** excel转pdf* @param inputStream* @param outputStream*/public static void excelToPdf(InputStream inputStream,OutputStream outputStream){ByteArrayInputStream licenseInputStream = null;try {//设置证书licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());com.aspose.cells.License wordLicense = new com.aspose.cells.License();wordLicense.setLicense(licenseInputStream);//转化Workbook workbook = new Workbook(inputStream);workbook.save(outputStream, 13);licenseInputStream.close();} catch (Exception e) {e.printStackTrace();System.out.println("excel转pdf失败");}}/*** ppt转pdf* @param inputStream* @param outputStream*/public static void pptToPdf(InputStream inputStream,OutputStream outputStream){ByteArrayInputStream licenseInputStream = null;try {//设置证书licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());com.aspose.slides.License wordLicense = new com.aspose.slides.License();wordLicense.setLicense(licenseInputStream);//转化Presentation presentation = new Presentation(inputStream);presentation.save(outputStream, 1);licenseInputStream.close();} catch (Exception e) {e.printStackTrace();System.out.println("ppt转pdf失败");}}/*** txt文档转pdf* @param inputStream* @param outputStream*/public static void txtToPdf(InputStream inputStream,OutputStream outputStream) {com.itextpdf.text.Document document = new com.itextpdf.text.Document();ClassPathResource classPathResource = new ClassPathResource("/font/simhei.ttf");try {PdfWriter.getInstance(document,outputStream);document.open();BaseFont baseFont = BaseFont.createFont(classPathResource.getPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font font = new Font(baseFont);InputStreamReader isr = new InputStreamReader(inputStream, "GBK");BufferedReader bufferedReader = new BufferedReader(isr);String str = "";while ((str = bufferedReader.readLine()) != null) {document.add(new Paragraph(str, font));}document.close();} catch (DocumentException e) {e.printStackTrace();System.out.println("txt转pdf失败");} catch (IOException e) {e.printStackTrace();}}/*** cad文件转pdf* @param inputStream* @param outputStream*/public static void cadToPdf(InputStream inputStream,OutputStream outputStream) {Image objImage = Image.load(inputStream);CadRasterizationOptions rasterizationOptions = new  CadRasterizationOptions();//设置属性rasterizationOptions.setBackgroundColor(Color.getBlack());rasterizationOptions.setPageWidth(1400);rasterizationOptions.setPageHeight(650);rasterizationOptions.setAutomaticLayoutsScaling(true);rasterizationOptions.setNoScaling (false);rasterizationOptions.setDrawType(1);PdfOptions pdfOptions = new PdfOptions();pdfOptions.setVectorRasterizationOptions(rasterizationOptions);objImage.save(outputStream,pdfOptions);objImage.close();}}

三、使用工具类

@ApiOperation(value = "下载:文件(pdf格式)",notes = "下载文件(pdf格式)")
@GetMapping("/download/{fileId}")
public void download(@ApiParam(value = "fileId",required = true)@PathVariable(value = "fileId",required = true)Integer fileId,HttpServletResponse response) {InputStream in;try {Map<String,Object> file = commonService.getMaterialFile(fileId);String fileName = MapUtil.getString(file,"FILENAME");String extension = MapUtil.getString(file,"EXTENSION");String newFileName = fileName.substring(0,fileName.lastIndexOf("."));//原始下载操作in = commonService.downloadMaterial(fileId);response.setContentType("application/x-msdownload;charset=utf-8");response.setHeader("Content-Disposition","attachment; filename=\"" + URLEncoder.encode(newFileName+".pdf","UTF-8")+ "\"");OutputStream out = response.getOutputStream();//pdf的直接返回文件流if ("pdf".equalsIgnoreCase(extension)) {IOUtils.copy(in,out);}else {//其他类型转为pdfPdfUtil.othersToPdf(extension,in,out);}out.close();in.close();}catch (IOException e) {e.printStackTrace();}
}

四、前端使用pdfjs预览pdf

1.引入js后在html定义一个div

<!-- 全屏弹出框=>文件预览     -->
<div id="dialogFull" class="bui-dialog" style="display: none;"><div ng-show="!notSupport" class="bui-dialog-head">{{filePopUpData.FILENAME}}</div><div ng-show="notSupport">当前文件不支持预览</div><!-- pdf文件预览 --><div class="bui-dialog-main" id="pdf-div" style="width: 100%;height: 900px"></div><div class="bui-dialog-close"><i class="icon-close"></i></div>
</div>

2.对应html的js中将文件下载地址按格式拼接在预览地址后

//固定格式:/static/pdfjs/web/viewer.html?file=+文件路径
url = "../static/pdfjs/web/viewer.html?file=" + fileUrl;
$('#pdf-div').media({src: url,width: "100%",height: "667px"
});
//弹出
DialogFull.open();

SpringBoot集成aspose实现cad、word、excle、ppt在线预览相关推荐

  1. word,excel,ppt在线预览功能

    我们在开发web项目时,尤其类似oa功能时总会遇到上传附件并在线预览的功能,发现一款api比较好使,下面简单介绍一下. 首先我利用自己的阿里云简单做了一个在线预览的demo,地址:http://app ...

  2. Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

    Spring Boot整合OpenOffice实现Word.Excel.PPT在线预览 1 介绍下OpenOffice 官网:https://www.openoffice.org/download/ ...

  3. KKFileView前端实现Word,Excel,PPT在线预览(实测可用于内网项目)

    目前网上现有的文件在线预览方式有以下几种: 1.通过a标签href属性直接打开文件 这种方式是通过把文件上传到服务器,在后台将文件转为pdf.通过浏览器可直接查看pdf文件的特点使用a标签直接就可以打 ...

  4. pdf,word,ppt在线预览

    pdf,word,ppt在线预览 先展示下效果 pdf跟ppt的预览效果: word的预览效果 实现过程-只需一个iframe标签即可 详细介绍请看这里 <iframe src="ht ...

  5. poi PPT 在线预览

    web 中在线预览office 是个头疼的问题. 今天项目刚好做完一个PPT在线预览的功能   在这里分享给大家   思路:      获取网络PPT 文件 将PPT每一页的幻灯片都转换成单张图片   ...

  6. 利用OpenOffice实现word文档在线预览

    项目中遇到的word文档在线预览需求,查阅很多资料决定利用openoffice转换word文档为pdf/html进行预览实现. 1.下载openoffice4安装 www.openoffice.org ...

  7. java实现word转pdf在线预览格式

    java实现word转pdf在线预览格式 前段时间的项目里涉及了此功能,调研过一些方案,踩过一些坑,一一总结在此. java转pdf的方案很多,但是很多都要收费,转pdf也有一些格式方面的问题. 方案 ...

  8. java word转pdf linux_java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)...

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

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

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

  10. java word在线预览_java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)...

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

最新文章

  1. linux shell ascii 字符 转换
  2. oracle的to_char中的fm
  3. JAVA每个初学者都应该搞懂的问题 还可以比较基础
  4. HDU 6096 AC自动机
  5. 安装memcache
  6. 结果不对_男子去医院抽血体检,拿到化验单发现不对劲,医院:医生专业不精...
  7. android之专栏目录
  8. 常见的IC封装形式大全(超详细)
  9. c++ virtual
  10. android系统cpu/内存信息提取设计
  11. 神棍传奇(cocos-jsV3.6)
  12. springboot后台搭建及登录注册接口编写
  13. 垂直投影法分割验证码
  14. C/C++实现http下载文件
  15. Bzoj4598: [Sdoi2016]模式字符串 点分治 哈希
  16. 合格的CTO应该是什么样?雷军王海峰王小川等共谈「技术创新」| CNCC2020-1
  17. c语言中int类型与char类型数据转换
  18. selenium网络爬虫去哪儿机票利用performance获取日志截获加载的xhr,ajax,js等数据
  19. FileUpload解析
  20. 2022-2028年全球与中国手机振动马达产业市场前瞻与投资战略规划分析

热门文章

  1. Facebook网络模拟测试工具ATC使用
  2. Hive 窗口函数 实现原理
  3. hive窗口函数练习题
  4. 成考期末计算机组成原理,计算机组成原理期末考试试题及答案
  5. deepin 命令行卸载软件
  6. Unity之Shader Pass 通道显示贴图的几种方法- 六
  7. oracle数据库拼接sql语句字符串问题
  8. sx1268 中文_微雪电子SX1268 Lora 433M频段简介
  9. activiti6执行Python脚本
  10. 王小波:我为什么要写作?