java实现在线预览功能(⽀持xlx,word,ppt,dwg等格式转Pdf)offoce转pdf⽂件预览,基于aspose-cad,aspose-cells,aspose-words,aspose-slides实现word,xls,ppt,dwg转pdf⽂件预览
之前基于openoffice做过⽂件转换预览,由于openoffice需要在容器安装服务⽽且不稳定,
容易因为内存问题,⽹络问题被挤掉或者⽆法连接和超时所有换aspose实现.
word,xls,ppt,dwg⼯具类
package versions.utils;
import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
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.Black());
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 Name().substring(Name().lastIndexOf("."))+".pdf");
//存放地址
try {
objImage.Path(), pdfOptions);
} catch (Exception e) {
e.printStackTrace();
<("dwg转pdf失败{}",Name());
} finally {
fileInputStream.close();
}
log.info("⽂件转换成功{}",Name());
return outputFile;
}
}
import lls.License;
import lls.PdfSaveOptions;
import lls.Workbook;
slf4j.Slf4j;
import java.io.*;
@Slf4j
public class ExcelToPdfUtil {
/**
* 获取license 去除⽔印
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = ClassLoader().getResourceAsStream("\\l");            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;
}
java streamByteArrayOutputStream 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 = ByteArray();
return outbytes;
} catch (Exception e) {
<("excel转pdf失败");
return null;
}finally {
if (pdfstream!=null){
pdfstream.close();
}
}
}
}
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
slf4j.Slf4j;
import java.io.*;
@Slf4j
public class PptToPdfUtil {
public static File ppt2pdf(File pptFile) throws IOException {
//输出⽂件
File outputFile = new Name().substring(Name().lastIndexOf("."))+".pdf");
/
/输⼊⽂件
FileInputStream fileInputStream = new FileInputStream(pptFile);
Presentation pres = new Presentation(fileInputStream);
try {
pres.Path(), SaveFormat.Pdf);
} catch (Exception e) {
e.printStackTrace();
<("ppt转pdf失败{}",Name());
} finally {
fileInputStream.close();
}
return outputFile;
}
}
package versions.utils;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
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 Name().substring(Name().lastIndexOf("."))+".pdf");        //输⼊⽂件
FileInputStream fileInputStream = new FileInputStream(wordFile);
Document doc = new Document(fileInputStream);
//存放地址
try {
doc.Path(),SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
<("word转pdf失败{}",Name());
}finally {
fileInputStream.close();
}
return outputFile;
}
}
实现
package versions.service.impl;
package versions.service.impl;
import io.FileUtil;
import util.IdUtil;
import util.StrUtil;
import com.onstant.SecurityConstants;
import com.fhzncloudmon.minio.service.MinioTemplate;
import fig.FileType;
import stant.BizServiceNameConstants;
import ity.ConversionQueue;
import com.ruifumon.feign.RemoteDocService;
import com.ruifumon.feign.RemoteProjService;
import com.ruifumon.feign.RemoteWorkSheetService;
import com.ruifumon.utils.FileUtils;
import com.ruifumon.utils.InputStream2byte;
import versions.service.FileConversionService;
import versions.utils.*;
slf4j.Slf4j;
import org.apachemons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
@Service
@Slf4j
public class FileConversionServiceImpl implements FileConversionService {
@Autowired
private MinioTemplate minioTemplate;
@Autowired
private RemoteProjService remoteProjService;
@Autowired
private RemoteDocService remoteDocService;
@Autowired
private RemoteWorkSheetService remoteWorkSheetService;
/**
* ⽂件转换
*
* @param conversionQueue
* @throws Exception
*/
@Override
public void conversionFileUpload(ConversionQueue conversionQueue){
//判断消息对象是否为空
if (null==conversionQueue) {
<("mq消息为空");
throw new RuntimeException("消息为空");
}
byte[] bytes = new byte[0];
try {
/
/输⼊流转字节数组
InputStream inputStream = VaultId(), Path());            bytes= InputStream2byte.inputStream2byte(inputStream);
} catch (Exception e) {
e.printStackTrace();
<("获取⽂件失败{}",SourceFileName());
}
//Byte数组转File
File sourceFile = FileUtils.ByteToFile(bytes, SourceFileName());
File sourceFile = FileUtils.ByteToFile(bytes, SourceFileName());
//获取⽂件后缀
String extName = Name());
//判断源⽂件后缀是否为xls
if (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();
<("office⽂件转换出错{}",Name());
}
}
private void getDocToPdf(ConversionQueue conversionQueue, File sourceFile) {
try {
//doc⽂件转换为pdf⽂件
File wordFile = WordToPdfUtil.word2pdf(sourceFile);
//上传附件
uploadAttachment(sourceFile, wordFile,conversionQueue);
} catch (Exception e) {
e.printStackTrace();
<("word⽂件转换出错{}",Name());
}
}
private void getDwgToPdf(ConversionQueue conversionQueue, File sourceFile) {
try {
//dwg⽂件转换为pdf⽂件
File dwgFile = DwgToPdfUtil.Dwg2Pdf(sourceFile);
//上传附件
uploadAttachment(sourceFile, dwgFile,conversionQueue);
} catch (IOException e) {
e.printStackTrace();
<("dwg⽂件转换出错{}",Name());
}
}
private void getXlsToPdf(ConversionQueue conversionQueue, byte[] bytes, File sourceFile) {
try {
byte[] xlsByte = l2pdf(bytes);
//⽂件名前缀+"."
String fileNamePrefix = SourceFileName().substring(0, SourceFileName().lastIndexOf(".")+1);            File xlsToFile = FileUtils.ByteToFile(xlsByte, fileNamePrefix+ FileType.PDF);
//上传附件
uploadAttachment(sourceFile, xlsToFile,conversionQueue);
} catch (IOException e) {
e.printStackTrace();
<("xsl⽂件转换出错{}",Name());
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。