feat: There is no way the convert
This commit is contained in:
@@ -1,25 +1,19 @@
|
||||
package com.adc.da.convert.common;
|
||||
|
||||
import com.artofsolving.jodconverter.DocumentConverter;
|
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
|
||||
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
|
||||
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
|
||||
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.jodconverter.DocumentConverter;
|
||||
import org.jodconverter.OfficeDocumentConverter;
|
||||
import org.jodconverter.document.DefaultDocumentFormatRegistry;
|
||||
import org.jodconverter.office.DefaultOfficeManagerBuilder;
|
||||
import org.jodconverter.office.OfficeException;
|
||||
import org.jodconverter.office.OfficeManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class DocConverterPdf {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DocConverterPdf.class);
|
||||
|
||||
@Autowired
|
||||
private DocumentConverter converter;
|
||||
|
||||
private static final int ENVIRONMENT = 1;// 环境 1:windows 2:linux
|
||||
private String fileString;// (只涉及pdf2swf路径问题)
|
||||
private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
|
||||
@@ -28,7 +22,6 @@ public class DocConverterPdf {
|
||||
private File convertPdfFile;
|
||||
private File swfFile;
|
||||
private File docFile;
|
||||
private String filesType;
|
||||
String waterPdfPath;
|
||||
|
||||
private String convertHost;
|
||||
@@ -56,7 +49,6 @@ public class DocConverterPdf {
|
||||
fileName = fileString.substring(0, fileString.lastIndexOf("."));
|
||||
if(".doc".equals(fileType.toLowerCase()) || ".docx".equals(fileType.toLowerCase()) || ".xlsx".equals(fileType.toLowerCase()) || ".xls".equals(fileType.toLowerCase()) || ".ppt".equals(fileType.toLowerCase()) || ".pptx".equals(fileType.toLowerCase())){
|
||||
docFile = new File(fileString);
|
||||
filesType = fileType.toLowerCase();
|
||||
convertPdfFile=new File(fileName+"_C"+ ".pdf");
|
||||
pdfFile = new File(fileName + ".pdf");
|
||||
swfFile = new File(fileName + ".swf");
|
||||
@@ -66,38 +58,6 @@ public class DocConverterPdf {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* openOffice目录,window 可能在C://Program Files (x86)//OpenOffice 4
|
||||
*/
|
||||
private static final String openOfficeHome = "/opt/openoffice4";
|
||||
|
||||
/**
|
||||
* openOffice端口
|
||||
*/
|
||||
private static final int openOfficePort = 8100;
|
||||
|
||||
/**
|
||||
* 转化文档
|
||||
* @param input
|
||||
* @param output
|
||||
*/
|
||||
public static void convertFile(File input , File output) throws OfficeException {
|
||||
try {
|
||||
DefaultOfficeManagerBuilder defaultOfficeManagerBuilder = new DefaultOfficeManagerBuilder();
|
||||
defaultOfficeManagerBuilder.setOfficeHome(openOfficeHome);
|
||||
OfficeManager officeManager = defaultOfficeManagerBuilder.build();
|
||||
officeManager.start();
|
||||
|
||||
//转换,自动识别转化类型
|
||||
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
|
||||
converter.convert(input, output);
|
||||
|
||||
officeManager.stop();
|
||||
} catch (OfficeException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为PDF
|
||||
*
|
||||
@@ -105,16 +65,22 @@ public class DocConverterPdf {
|
||||
*/
|
||||
private void doc2pdf() throws Exception {
|
||||
if (!pdfFile.exists()) {
|
||||
OpenOfficeConnection connection = null;
|
||||
logger.info("转换服务地址接口:"+convertHost);
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
try {
|
||||
// in = new FileInputStream(docFile);
|
||||
// out = new FileOutputStream(convertPdfFile);
|
||||
convertFile(docFile,convertPdfFile);
|
||||
// converter.convert(in).as(DefaultDocumentFormatRegistry.getFormatByMediaType(filesType)).
|
||||
// to(out).as(DefaultDocumentFormatRegistry.PDF);
|
||||
if(StringUtils.isNotBlank(convertHost)){
|
||||
connection = new SocketOpenOfficeConnection(convertHost,8100);
|
||||
}else{
|
||||
connection = new SocketOpenOfficeConnection(8100);
|
||||
}
|
||||
connection.connect();
|
||||
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
|
||||
converter.convert(docFile, convertPdfFile);
|
||||
|
||||
//addWaterToPdf(pdfFile,userId);
|
||||
|
||||
// close the connection
|
||||
connection.disconnect();
|
||||
// 开始设置PDF文档相关属性保证其安全性
|
||||
logger.info("开始设置转换后的PDF文件安全性相关设置");
|
||||
PDFEditUtils.setPDFFileSecurity(pdfFile.getAbsolutePath(),convertPdfFile.getAbsolutePath());
|
||||
@@ -123,6 +89,10 @@ public class DocConverterPdf {
|
||||
logger.error(e.getMessage(),e);
|
||||
logger.info("****pdf转换器异常,openoffice服务未启动!****");
|
||||
throw e;
|
||||
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
logger.info("****pdf转换器异常,读取转换文件失败****");
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
throw e;
|
||||
|
||||
Reference in New Issue
Block a user