offoce转pdf文件预览,基于aspose-cad,aspose-cells,aspose-words,aspose-slides实现word,xls,ppt,dwg转pdf文件预览

之前基于openoffice做过文件转换预览,由于openoffice需要在容器安装服务而且不稳定,
容易因为内存问题,网络问题被挤掉或者无法连接和超时所有换aspose实现.

word,xls,ppt,dwg工具类

package com.ruifu.conversions.utils;import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;@Slf4j
public class DwgToPdfUtil {public static File Dwg2Pdf(File dwgFile) throws IOException {FileInputStream fileInputStream;//将dwg文件转换成InputStream输入流fileInputStream = new FileInputStream(dwgFile);Image objImage = Image.load(fileInputStream);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);//输出文件File outputFile = new File(dwgFile.getName().substring(0,dwgFile.getName().lastIndexOf("."))+".pdf");//存放地址try {objImage.save(outputFile.getPath(), pdfOptions);} catch (Exception e) {e.printStackTrace();log.error("dwg转pdf失败{}",dwgFile.getName());} finally {fileInputStream.close();}log.info("文件转换成功{}",dwgFile.getName());return outputFile;}
}
package com.ruifu.conversions.utils;import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import lombok.extern.slf4j.Slf4j;import java.io.*;@Slf4j
public class ExcelToPdfUtil {/*** 获取license 去除水印* @return*/public static boolean getLicense() {boolean result = false;try {InputStream is = ExcelToPdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** excel转pdf* @param bytes* @return* @throws IOException*/public static byte[] excel2pdf(byte[] bytes) throws IOException {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return null;}ByteArrayOutputStream pdfstream=null;try {// 原始excel流Workbook wb = new Workbook(new ByteArrayInputStream(bytes));//保存转成pdf的流pdfstream = new ByteArrayOutputStream();PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();//缩放到一个页面(如果列太多 太长)设置每张一页pdfSaveOptions.setOnePagePerSheet(true);wb.save(pdfstream, pdfSaveOptions);byte[] outbytes = pdfstream.toByteArray();return outbytes;} catch (Exception e) {log.error("excel转pdf失败");return null;}finally {if (pdfstream!=null){pdfstream.close();}}}
}
package com.ruifu.conversions.utils;import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import lombok.extern.slf4j.Slf4j;import java.io.*;@Slf4j
public class PptToPdfUtil {public static File ppt2pdf(File pptFile) throws IOException {//输出文件File outputFile = new File(pptFile.getName().substring(0,pptFile.getName().lastIndexOf("."))+".pdf");//输入文件FileInputStream fileInputStream = new FileInputStream(pptFile);Presentation pres = new Presentation(fileInputStream);try {pres.save(outputFile.getPath(), SaveFormat.Pdf);} catch (Exception e) {e.printStackTrace();log.error("ppt转pdf失败{}",pptFile.getName());} finally {fileInputStream.close();}return outputFile;}
}
package com.ruifu.conversions.utils;import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.FileInputStream;@Slf4j
public class WordToPdfUtil {public static File word2pdf(File wordFile) throws Exception {//输出文件File outputFile = new File(wordFile.getName().substring(0,wordFile.getName().lastIndexOf("."))+".pdf");//输入文件FileInputStream fileInputStream = new FileInputStream(wordFile);Document doc = new Document(fileInputStream);//存放地址try {doc.save(outputFile.getPath(),SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();log.error("word转pdf失败{}",wordFile.getName());}finally {fileInputStream.close();}return outputFile;}
}

实现

package com.ruifu.conversions.service.impl;import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.fhzncloud.cloud.common.core.constant.SecurityConstants;
import com.fhzncloud.common.minio.service.MinioTemplate;
import com.ruifu.common.config.FileType;
import com.ruifu.common.constant.BizServiceNameConstants;
import com.ruifu.common.eneity.ConversionQueue;
import com.ruifu.common.feign.RemoteDocService;
import com.ruifu.common.feign.RemoteProjService;
import com.ruifu.common.feign.RemoteWorkSheetService;
import com.ruifu.common.utils.FileUtils;
import com.ruifu.common.utils.InputStream2byte;
import com.ruifu.conversions.service.FileConversionService;
import com.ruifu.conversions.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.*;@Service
@Slf4j
public class FileConversionServiceImpl implements FileConversionService {@Autowiredprivate MinioTemplate minioTemplate;@Autowiredprivate RemoteProjService remoteProjService;@Autowiredprivate RemoteDocService remoteDocService;@Autowiredprivate RemoteWorkSheetService remoteWorkSheetService;/*** 文件转换** @param conversionQueue* @throws Exception*/@Overridepublic void conversionFileUpload(ConversionQueue conversionQueue){//判断消息对象是否为空if (null==conversionQueue) {log.error("mq消息为空");throw new RuntimeException("消息为空");}byte[] bytes = new byte[0];try {//输入流转字节数组InputStream inputStream = minioTemplate.getObject(conversionQueue.getVaultId(), conversionQueue.getPath());bytes= InputStream2byte.inputStream2byte(inputStream);} catch (Exception e) {e.printStackTrace();log.error("获取文件失败{}",conversionQueue.getSourceFileName());}//Byte数组转FileFile sourceFile = FileUtils.ByteToFile(bytes, conversionQueue.getSourceFileName());//获取文件后缀String extName = FilenameUtils.getExtension(sourceFile.getName());//判断源文件后缀是否为xlsif (FileType.XLSX.equalsIgnoreCase(extName) || FileType.XLS.equalsIgnoreCase(extName)){getXlsToPdf(conversionQueue, bytes, sourceFile);}else if (FileType.DWG.equalsIgnoreCase(extName)){getDwgToPdf(conversionQueue, sourceFile);}else if (FileType.DOCX.equalsIgnoreCase(extName) || FileType.DOC.equalsIgnoreCase(extName)){getDocToPdf(conversionQueue, sourceFile);}else if (FileType.PPTX.equalsIgnoreCase(extName) || FileType.PPT.equalsIgnoreCase(extName)){getPptToPdf(conversionQueue, sourceFile);}}private void getPptToPdf(ConversionQueue conversionQueue, File sourceFile) {try {//ppt文件转换为pdf文件File pptFile = PptToPdfUtil.ppt2pdf(sourceFile);//上传附件uploadAttachment(sourceFile, pptFile,conversionQueue);} catch (Exception e) {e.printStackTrace();log.error("office文件转换出错{}",sourceFile.getName());}}private void getDocToPdf(ConversionQueue conversionQueue, File sourceFile) {try {//doc文件转换为pdf文件File wordFile = WordToPdfUtil.word2pdf(sourceFile);//上传附件uploadAttachment(sourceFile, wordFile,conversionQueue);} catch (Exception e) {e.printStackTrace();log.error("word文件转换出错{}",sourceFile.getName());}}private void getDwgToPdf(ConversionQueue conversionQueue, File sourceFile) {try {//dwg文件转换为pdf文件File dwgFile = DwgToPdfUtil.Dwg2Pdf(sourceFile);//上传附件uploadAttachment(sourceFile, dwgFile,conversionQueue);} catch (IOException e) {e.printStackTrace();log.error("dwg文件转换出错{}",sourceFile.getName());}}private void getXlsToPdf(ConversionQueue conversionQueue, byte[] bytes, File sourceFile) {try {byte[] xlsByte = ExcelToPdfUtil.excel2pdf(bytes);//文件名前缀+"."String fileNamePrefix = conversionQueue.getSourceFileName().substring(0, conversionQueue.getSourceFileName().lastIndexOf(".")+1);File xlsToFile = FileUtils.ByteToFile(xlsByte, fileNamePrefix+ FileType.PDF);//上传附件uploadAttachment(sourceFile, xlsToFile,conversionQueue);} catch (IOException e) {e.printStackTrace();log.error("xsl文件转换出错{}",sourceFile.getName());}}/*** 附件上传* @param sourceFile 源文件* @param annexFile  附件* @param conversionQueue 消息对象*/private void uploadAttachment(File sourceFile, File annexFile,ConversionQueue conversionQueue) {if (annexFile.exists()) {log.info("文件转换完成{}",annexFile.getName());//删除源文件if (sourceFile.exists()) {if (sourceFile.delete()){log.info("源文件删除成功{}",sourceFile.getName());}}else {log.info("源文件删除失败{}",sourceFile.getName());}}FileInputStream fileInputStream = null;try {//将pdf文件转换成InputStream输入流fileInputStream = new FileInputStream(annexFile);} catch (FileNotFoundException e) {e.printStackTrace();}log.info("文件{}大小为:{}",annexFile.getName(),annexFile.length());try {String extName = FileUtil.extName(annexFile.getName());String path = IdUtil.simpleUUID() + StrUtil.DOT + extName;//上传minioTemplate.putObject(conversionQueue.getVaultId(), path, fileInputStream,annexFile.length(),FileContentTypeUtils.contentType("."+extName));log.info("附件上传成功{}",annexFile.getName());//上传成功远程写入try {getServiceRemote(conversionQueue.getServiceName(),conversionQueue.getSourceFileId(),annexFile.length(),annexFile.getName(),path);} catch (Exception e) {e.printStackTrace();log.error("远程调用附件初始化失败{}",annexFile.getName());}} catch (Exception e) {e.printStackTrace();log.error("转换文件上传失败{}",annexFile.getName());}try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();log.error("fileInputStream关闭失败");}finally {if (annexFile.delete()){log.info("临时文件删除成功{}",annexFile.getName());}}}/*** 更新附件到数据库* @param serviceName* @param sourceFileId* @param fileSize* @param newFileName* @param path*/public void getServiceRemote(String serviceName,Long sourceFileId,Long fileSize,String newFileName,String path) {switch (serviceName){case BizServiceNameConstants.PROJECT_SERVICE:remoteProjService.initializationFileInfo(sourceFileId, fileSize, newFileName, path, SecurityConstants.FROM_IN);//远程调用项目任务服务break;case BizServiceNameConstants.DOC_SERVICE://远程调用文档服务remoteDocService.initializationFileInfo(sourceFileId, fileSize, newFileName, path, SecurityConstants.FROM_IN);break;case BizServiceNameConstants.WORKSHEET_SERVICE://远程调用变更服务break;default:break;}}
}

总结

优点:比openoffice好一点,不需要再去安装openoffice服务以及远程连接openoffice了,
效果的话xls能分页展示展示多个sheet比openoffice的A4裁切好上不少.
缺点:优点吃内存,需要调整jvm内存分配,其次就是有水印(虽然可以去掉)

java实现在线预览功能(支持xlx,word,ppt,dwg等格式转Pdf)相关推荐

  1. Java实现在线预览功能

    java实现在线预览功能,需要用到  jacob.dll jacob.jar   预览pdf所需js  pdfobject.min.js 将上传文件转为pdf保存. <divclass=&quo ...

  2. 用微软接口实现在线预览功能+iframe

    我们有的时候需要在线预览功能:2-doc/docx/ppt等office文件 1:文件类型filetype:1-html/pdf/png/jpeg等 {type == 1 && < ...

  3. 快速实现word、excel、ppt、txt等办公文件在线预览功能(Java版)

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/weixin_40986713/ article/details/109527294 java实现办公文件在线预览功能是一个大家 ...

  4. java零碎要点010---Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  5. Java 实现word、excel、ppt、txt等办公文件在线预览功能!

    大家好,我是宝哥! 如何用 Java 实现word.excel.ppt.txt等办公文件在线预览功能?本文告诉你答案! java 实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司 ...

  6. 手把手教你用 Java 实现word、excel、ppt、txt等办公文件在线预览功能!

    如何用 Java 实现word.excel.ppt.txt等办公文件在线预览功能?本文告诉你答案! java 实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务, ...

  7. java 预览word文档_Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  8. 前端【vue】实现文档在线预览功能,在线预览pdf、word、xls、ppt等office文件

    前端江太公 前端实现文档在线预览功能 最直接的就是使用XDOC 文档云服务 XDOC可以实现预览以DataURI表示的DOC文档,此外XDOC还可以实现文本.带参数文本.html文本.json文本.公 ...

  9. office 文档 在线预览功能实现(word,excel,pdf,ppt等多种格式)——使用https://view.xdocin.com/view 提示文档过期——基础积累

    web实现office文档在线预览功能--基础积累 最近遇到一个需求,就是要实现多种文档链接的在线预览,最简单的方式就是通过window.open(url地址)的方式来实现. 但是如果要求是在一个弹窗 ...

最新文章

  1. 泛型java 代码讲解_Java泛型详解
  2. 尚硅谷学Javaweb,关于正则表达式笔记
  3. pg数据库生成随机时间_postgresql 时区与时间函数-阿里云开发者社区
  4. [Hadoop in China 2011] Facebook Message在HBase基础上的应用
  5. python制作ios游戏_python自动化生成IOS的图标
  6. Node版本管理nvm的用法
  7. ddmmyy日期格式是多少_如何在Excel 2013/2016/2019中将mmddyyyy文本转换为普通日期格式...
  8. Linux-firewalld-squid正向代理
  9. RDCMan安装使用说明
  10. 生来只为丈量天空,开普勒的传奇一生
  11. 课题设计T25,使用51单片机设计一个里程计数器,Proteus设计,keil程序
  12. Java面试题上篇(转)
  13. Win10系统文件备份方法汇总
  14. Python中的pandas库简介及其使用
  15. 设计模式——行为型模式_观察者模式/发布-订阅模式
  16. Spark伪分布式搭建与SparkYarn搭建
  17. 如何提高个人理解能力?表达能力?分析能力?
  18. php javascript wav波形绘制,php分析.wav文件并绘制png格式的波形图_php技巧
  19. mysql基础操作和查询语句
  20. Nhiberate了解

热门文章

  1. 解析Kafka中的 Topic 和 Partition
  2. 绝对零度!冷原子量子计算机技术的6大优势
  3. 周末北大学拳散记--搜狐畅游招聘
  4. 基于JavaScript的HTML用户注册界面以及简单应用
  5. 雅虎通网络摄像头被黑客攻击
  6. oracle调用apps,oracleEBS 调用 SHELL 的方法
  7. for迭代求折纸超过珠峰高度
  8. [Vulnhub]Momentum2
  9. 九度oj 第1题 二维数组中的查找 何海涛:《剑指Offer:名企面试官精讲典型编程题》
  10. 国内骨干网互联互通格局巨变,中移动将与电信和联通免费对等互联