分离tl-server和server中接口的非公共方法,将公共方法wordToPdf放到BaseDocumentService中
BaseDocumentService.java中增加doc转docx、xls转xlsx的方法 BaseDocumentService.java中增加convert方法
This commit is contained in:
@@ -0,0 +1,52 @@
|
|||||||
|
package com.optima.document.api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yanghuanglin
|
||||||
|
* @since 2022/12/28
|
||||||
|
*/
|
||||||
|
public interface BaseDocumentService {
|
||||||
|
/**
|
||||||
|
* 格式转换,server和tl-server下均实现
|
||||||
|
*
|
||||||
|
* @param sourceData 源文件流,tl-server下仅支持docx格式
|
||||||
|
* @param sourceExtension 源文件后缀名,不包含"."
|
||||||
|
* @param targetExtension 目标文件后缀名,不包含"."
|
||||||
|
* @param targetFormat 目标文件格式
|
||||||
|
* @return 转换后的文件流
|
||||||
|
*/
|
||||||
|
default byte[] convert(byte[] sourceData, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通word转pdf,server和tl-server下均实现
|
||||||
|
*
|
||||||
|
* @param source 源文件流,tl-server下仅支持docx格式
|
||||||
|
* @param sourceFormat 源文件后缀名,不包含".",tl-server下可忽略
|
||||||
|
* @param clear 是否清除占位符
|
||||||
|
* @return pdf文档流
|
||||||
|
*/
|
||||||
|
default byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* doc转为docx,server和tl-server下均实现
|
||||||
|
*
|
||||||
|
* @param docData doc文档流
|
||||||
|
* @return docx文档流
|
||||||
|
*/
|
||||||
|
default byte[] docToDocx(byte[] docData) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xls转为xlsx,server和tl-server下均实现
|
||||||
|
*
|
||||||
|
* @param xlsData xls文档流
|
||||||
|
* @return xlsx文档流
|
||||||
|
*/
|
||||||
|
default byte[] xlsToXlsx(byte[] xlsData) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,71 +1,44 @@
|
|||||||
package com.optima.document.api;
|
package com.optima.document.api;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文档接口
|
* 文档接口,此类中用的poi2生成word文档,使用docto进行格式转换
|
||||||
* @author Elias
|
*
|
||||||
* @date 2021-09-28 16:00
|
* @author yanghuanglin
|
||||||
|
* @since 2022/12/28
|
||||||
*/
|
*/
|
||||||
public interface DocumentService {
|
public interface DocumentService extends BaseDocumentService {
|
||||||
/**
|
/**
|
||||||
* generate word
|
* 通过调用poi生成word,仅tl-server模块下实现,仅支持docx格式
|
||||||
* @param templateData word模版流
|
*
|
||||||
|
* @param templateData word模版流,仅支持docx格式
|
||||||
* @param dataModel 数据模型
|
* @param dataModel 数据模型
|
||||||
* @return 修改后的文档流
|
* @return word文档流
|
||||||
*/
|
*/
|
||||||
default byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
|
default byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* word to pdf
|
* 通过调用poi将word转pdf,仅tl-server模块下实现,仅支持docx格式
|
||||||
* @param templateData word模版流
|
*
|
||||||
|
* @param templateData word模版流,仅支持docx格式
|
||||||
* @param clear 是否清除占位符
|
* @param clear 是否清除占位符
|
||||||
* @return
|
* @return pdf文档流
|
||||||
*/
|
*/
|
||||||
default byte[] wordToPdf(byte[] templateData, boolean clear) {
|
default byte[] wordToPdf(byte[] templateData, boolean clear) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* word to image
|
* word转图片,仅支持docx格式
|
||||||
* @param templateData word模版流
|
|
||||||
* @param targetFormat 目标格式 支持jpeg, jpg, gif, tiff or png
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
default byte[] wordToImage(byte[] templateData, String targetFormat) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
*
|
||||||
* @param source 文档
|
* @param source word文件流,仅支持docx格式
|
||||||
* @param toFindText 需要替换的文本
|
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
|
||||||
* @param imgSource 图片
|
* @return 图片流
|
||||||
* @param width 宽度
|
|
||||||
* @param height 高度
|
|
||||||
* @return 修改后的文档
|
|
||||||
*/
|
*/
|
||||||
default byte[] insertJpeg(byte[] source, String toFindText, byte[] imgSource, int width, int height){
|
default byte[] wordToImage(byte[] source, String targetExtension) {
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param source
|
|
||||||
* @param toFindText
|
|
||||||
* @param imgSource
|
|
||||||
* @param width
|
|
||||||
* @param height
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
default byte[] insertJpeg(byte[] source, String toFindText, List<byte[]> imgSource, int width, int height) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
default byte[] fieldToWord(byte[] source, Map<String, Object> infoMap) {
|
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package com.optima.document.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文档接口,此类中用的jacob生成word文档,插入图片等,使用openoffice进行格式转换
|
||||||
|
*
|
||||||
|
* @author Elias
|
||||||
|
* @since 2021-09-28 16:00
|
||||||
|
*/
|
||||||
|
public interface LegacyDocumentService extends BaseDocumentService {
|
||||||
|
/**
|
||||||
|
* 通过pdfbox将word转图片,仅server模块下实现
|
||||||
|
*
|
||||||
|
* @param templateData word模版流
|
||||||
|
* @param sourceExtension 源文件后缀名,不包含"."
|
||||||
|
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
|
||||||
|
* @return 图片流
|
||||||
|
*/
|
||||||
|
default byte[] wordToImage(byte[] templateData, String sourceExtension, String targetExtension) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过jacob向文档中插入图片,仅server模块下实现
|
||||||
|
*
|
||||||
|
* @param source 文档流
|
||||||
|
* @param toFindText 需要替换的文本
|
||||||
|
* @param imgSource 图片流
|
||||||
|
* @param width 宽度
|
||||||
|
* @param height 高度
|
||||||
|
* @return word文档流
|
||||||
|
*/
|
||||||
|
default byte[] insertJpeg(byte[] source, String toFindText, byte[] imgSource, int width, int height) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过jacob向文档中插入多张图片,仅server模块下实现
|
||||||
|
*
|
||||||
|
* @param source 文档流
|
||||||
|
* @param toFindText 需要替换的文本
|
||||||
|
* @param imgSource 图片流列表
|
||||||
|
* @param width 宽度
|
||||||
|
* @param height 高度
|
||||||
|
* @return word文档流
|
||||||
|
*/
|
||||||
|
default byte[] insertJpeg(byte[] source, String toFindText, List<byte[]> imgSource, int width, int height) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过jacob向word填充属性字段,仅server模块下实现
|
||||||
|
*
|
||||||
|
* @param source 文档流
|
||||||
|
* @param infoMap 字段集合,${key}为占位字符,value为对应替换内容
|
||||||
|
* @return word文档流
|
||||||
|
*/
|
||||||
|
default byte[] fieldToWord(byte[] source, Map<String, Object> infoMap) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,14 +5,17 @@ import com.artofsolving.jodconverter.DocumentConverter;
|
|||||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
|
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
|
||||||
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
|
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
|
||||||
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
|
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.LegacyDocumentService;
|
||||||
|
import com.optima.document.server.config.DocumentConfig;
|
||||||
import com.optima.document.server.utils.DocToPdfUtil;
|
import com.optima.document.server.utils.DocToPdfUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -21,11 +24,44 @@ import java.util.Map;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务接口实现
|
* 服务接口实现
|
||||||
|
*
|
||||||
* @author Elias
|
* @author Elias
|
||||||
* @date 2021-09-28 16:18
|
* @since 2021-09-28 16:18
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class DocumentServiceImpl implements DocumentService {
|
@Service
|
||||||
|
public class LegacyDocumentServiceImpl implements LegacyDocumentService {
|
||||||
|
@Resource
|
||||||
|
private DocumentConfig documentConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件格式转换
|
||||||
|
*
|
||||||
|
* @param source 源文件流
|
||||||
|
* @param sourceExtension 源文件后缀名,不包含"."
|
||||||
|
* @param targetExtension 目标文件后缀名
|
||||||
|
* @return 转换后的文件流
|
||||||
|
*/
|
||||||
|
private byte[] convert(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
|
try {
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
String command = "%s -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
|
||||||
|
Process p = Runtime.getRuntime().exec(String.format(command, documentConfig.getOpenOfficeHome()));
|
||||||
|
OpenOfficeConnection connection = new SocketOpenOfficeConnection();
|
||||||
|
connection.connect();
|
||||||
|
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
DefaultDocumentFormatRegistry formatRegistry = new DefaultDocumentFormatRegistry();
|
||||||
|
converter.convert(new ByteArrayInputStream(source), formatRegistry.getFormatByFileExtension(sourceExtension), bos, formatRegistry.getFormatByFileExtension(targetExtension));
|
||||||
|
connection.disconnect();
|
||||||
|
p.destroy();
|
||||||
|
log.info("{} to {} take {} milliseconds", sourceExtension, targetExtension, System.currentTimeMillis() - start);
|
||||||
|
return bos.toByteArray();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] fieldToWord(byte[] source, Map<String, Object> infoMap) {
|
public byte[] fieldToWord(byte[] source, Map<String, Object> infoMap) {
|
||||||
return DocToPdfUtil.fieldToWord(source, infoMap);
|
return DocToPdfUtil.fieldToWord(source, infoMap);
|
||||||
@@ -39,39 +75,31 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return DocToPdfUtil.insertJpeg(source, toFindText, imgSource, width, height);
|
return DocToPdfUtil.insertJpeg(source, toFindText, imgSource, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
public byte[] wordToPdf(byte[] source, String sourceExtension, boolean clear) {
|
||||||
try {
|
try {
|
||||||
if (clear) {
|
if (clear) {
|
||||||
source = DocToPdfUtil.clearPlaceholder(source);
|
source = DocToPdfUtil.clearPlaceholder(source);
|
||||||
}
|
}
|
||||||
long t1 = System.currentTimeMillis();
|
return convert(source, sourceExtension, "pdf");
|
||||||
String command = "D:\\OpenOffice4\\program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
|
|
||||||
Process p = Runtime.getRuntime().exec(command);
|
|
||||||
OpenOfficeConnection connection = new SocketOpenOfficeConnection();
|
|
||||||
connection.connect();
|
|
||||||
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
||||||
DefaultDocumentFormatRegistry formatRegistry = new DefaultDocumentFormatRegistry();
|
|
||||||
converter.convert(new ByteArrayInputStream(source), formatRegistry.getFormatByFileExtension(sourceFormat), bos, formatRegistry.getFormatByFileExtension("pdf"));
|
|
||||||
connection.disconnect();
|
|
||||||
p.destroy();
|
|
||||||
log.info("word to pdf=======consuming:{} milliseconds", System.currentTimeMillis() - t1);
|
|
||||||
return bos.toByteArray();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToImage(byte[] source, String sourceFormat, String targetFormat) {
|
public byte[] convert(byte[] sourceData, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
|
return convert(sourceData, sourceExtension, targetExtension);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
try {
|
try {
|
||||||
byte[] pdfBytes = wordToPdf(source, sourceFormat, true);
|
byte[] pdfBytes = wordToPdf(source, sourceExtension, true);
|
||||||
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
|
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
BufferedImage bim = pdfRenderer.renderImageWithDPI(
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(
|
||||||
0, 300, ImageType.RGB);
|
0, 300, ImageType.RGB);
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
ImageIOUtil.writeImage(bim, targetFormat, bos, 300);
|
ImageIOUtil.writeImage(bim, targetExtension, bos, 300);
|
||||||
document.close();
|
document.close();
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -79,4 +107,12 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] docToDocx(byte[] docData) {
|
||||||
|
return convert(docData, "doc", "docx");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] xlsToXlsx(byte[] xlsData) {
|
||||||
|
return convert(xlsData, "xls", "xlsx");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.optima.document.server.config;
|
package com.optima.document.server.config;
|
||||||
|
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.LegacyDocumentService;
|
||||||
import com.optima.document.server.api.DocumentServiceImpl;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -10,9 +9,11 @@ import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端配置
|
* 服务端配置
|
||||||
|
*
|
||||||
* @author Elias
|
* @author Elias
|
||||||
* @date 2021-09-28 16:12
|
* @since 2021-09-28 16:12
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("VulnerableCodeUsages")
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "document")
|
@ConfigurationProperties(prefix = "document")
|
||||||
@Data
|
@Data
|
||||||
@@ -24,13 +25,14 @@ public class DocumentConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文档接口
|
* 文档接口
|
||||||
|
*
|
||||||
* @return httpinvoker
|
* @return httpinvoker
|
||||||
*/
|
*/
|
||||||
@Bean(name = "/document-service")
|
@Bean(name = "/document-service")
|
||||||
HttpInvokerServiceExporter wordService() {
|
HttpInvokerServiceExporter wordService(LegacyDocumentService legacyDocumentService) {
|
||||||
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
|
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
|
||||||
exporter.setService( new DocumentServiceImpl() );
|
exporter.setService(legacyDocumentService);
|
||||||
exporter.setServiceInterface(DocumentService.class);
|
exporter.setServiceInterface(LegacyDocumentService.class);
|
||||||
return exporter;
|
return exporter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8090
|
port: 8090
|
||||||
document:
|
document:
|
||||||
doc-to-program: D:\\optima\\docto.exe
|
open-office-home: D:\\OpenOffice4\\program\\soffice.exe
|
||||||
|
|
||||||
|
|||||||
@@ -13,30 +13,33 @@ import com.deepoove.poi.xwpf.BodyContainerFactory;
|
|||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.DocumentService;
|
||||||
import com.optima.document.tl.server.config.DocumentConfig;
|
import com.optima.document.tl.server.config.DocumentConfig;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务接口实现
|
* 服务接口实现
|
||||||
|
*
|
||||||
* @author Elias
|
* @author Elias
|
||||||
* @date 2021-09-28 16:18
|
* @since 2021-09-28 16:18
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Service
|
||||||
public class DocumentServiceImpl implements DocumentService {
|
public class DocumentServiceImpl implements DocumentService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,11 +86,48 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
@Resource
|
@Resource
|
||||||
private DocumentConfig documentConfig;
|
private DocumentConfig documentConfig;
|
||||||
|
|
||||||
@Override
|
public byte[] convert(byte[] sourceData, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
|
try {
|
||||||
|
if (!sourceExtension.contains("."))
|
||||||
|
sourceExtension = "." + sourceExtension;
|
||||||
|
if (!targetExtension.contains("."))
|
||||||
|
targetExtension = "." + targetExtension;
|
||||||
|
Path sourcePath = Files.createTempFile(UUID.randomUUID().toString(), sourceExtension);
|
||||||
|
Path targetPath = Files.createTempFile(UUID.randomUUID().toString(), targetExtension);
|
||||||
|
Files.write(sourcePath, sourceData);
|
||||||
|
try {
|
||||||
|
long begin = System.currentTimeMillis();
|
||||||
|
String command;
|
||||||
|
if (sourceExtension.contains("xls")) {
|
||||||
|
command = "%s -XL -f %s -O %s -T %s";
|
||||||
|
} else if (sourceExtension.contains("ppt")) {
|
||||||
|
command = "%s -PP -f %s -O %s -T %s";
|
||||||
|
} else {
|
||||||
|
command = "%s -WD -f %s -O %s -T %s";
|
||||||
|
}
|
||||||
|
command = String.format(command, documentConfig.getDocToProgram(), sourcePath, targetPath, targetFormat);
|
||||||
|
Process p = Runtime.getRuntime().exec(command);
|
||||||
|
p.waitFor();
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
log.info("docto {} to {} take time in millis:{}", sourceExtension, targetExtension, (end - begin));
|
||||||
|
return Files.readAllBytes(targetPath);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
Files.delete(sourcePath);
|
||||||
|
Files.delete(targetPath);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("docto convert error", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
|
public byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
XWPFTemplate template = XWPFTemplate.compile(new ByteArrayInputStream(templateData), wtlConfig).render(dataModel);
|
XWPFTemplate template = XWPFTemplate.compile(new ByteArrayInputStream(templateData), wtlConfig).render(dataModel);
|
||||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()){
|
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||||
template.write(bos);
|
template.write(bos);
|
||||||
log.info("word generate========consuming:{} milliseconds", System.currentTimeMillis() - start);
|
log.info("word generate========consuming:{} milliseconds", System.currentTimeMillis() - start);
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
@@ -97,7 +137,10 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
||||||
|
return wordToPdf(source, clear);
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] wordToPdf(byte[] source, boolean clear) {
|
public byte[] wordToPdf(byte[] source, boolean clear) {
|
||||||
try {
|
try {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
@@ -119,52 +162,36 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Path tempFilePath = Files.createTempFile(UUID.randomUUID().toString(), ".docx");
|
return convert(source, "docx", "pdf", "wdFormatPDF");
|
||||||
Files.write(tempFilePath, source);
|
|
||||||
|
|
||||||
Path pdfPath = Files.createTempFile(UUID.randomUUID().toString(), ".pdf");
|
|
||||||
|
|
||||||
try {
|
|
||||||
long begin = System.currentTimeMillis();
|
|
||||||
String command = "%s -f %s -O %s -T wdFormatPDF";
|
|
||||||
command = String.format(command, documentConfig.getDocToProgram(), tempFilePath, pdfPath);
|
|
||||||
Process p = Runtime.getRuntime().exec(command);
|
|
||||||
p.waitFor();
|
|
||||||
long end = System.currentTimeMillis();
|
|
||||||
log.info("docto take time in millis:" + (end - begin));
|
|
||||||
return Files.readAllBytes(pdfPath);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
Files.delete(tempFilePath);
|
|
||||||
Files.delete(pdfPath);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("word to pdf error", e);
|
log.error("word to pdf error", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public byte[] wordToImage(byte[] source, String targetExtension) {
|
||||||
public byte[] wordToImage(byte[] source, String targetFormat) {
|
|
||||||
try {
|
try {
|
||||||
byte[] pdfBytes = wordToPdf(source, true);
|
byte[] pdfBytes = wordToPdf(source, true);
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
|
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
BufferedImage bim = pdfRenderer.renderImageWithDPI(
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
|
||||||
0, 300, ImageType.RGB);
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
ImageIOUtil.writeImage(bim, targetFormat, bos, 300);
|
ImageIOUtil.writeImage(bim, targetExtension, bos, 300);
|
||||||
document.close();
|
document.close();
|
||||||
log.info("word to image=======consuming:{} milliseconds", System.currentTimeMillis() - start);
|
log.info("word to image=======consuming:{} milliseconds", System.currentTimeMillis() - start);
|
||||||
|
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error("word to image error", e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] docToDocx(byte[] docData) {
|
||||||
|
return convert(docData, "doc", "docx", "wdFormatDocumentDefault");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] xlsToXlsx(byte[] xlsData) {
|
||||||
|
return convert(xlsData, "xls", "xlsx", "xlWorkbookDefault");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,19 @@
|
|||||||
package com.optima.document.tl.server.config;
|
package com.optima.document.tl.server.config;
|
||||||
|
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.DocumentService;
|
||||||
import com.optima.document.tl.server.api.DocumentServiceImpl;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
|
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.net.Socket;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端配置
|
* 服务端配置
|
||||||
|
*
|
||||||
* @author Elias
|
* @author Elias
|
||||||
* @date 2021-09-28 16:12
|
* @since 2021-09-28 16:12
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("VulnerableCodeUsages")
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "document")
|
@ConfigurationProperties(prefix = "document")
|
||||||
@Data
|
@Data
|
||||||
@@ -28,12 +23,13 @@ public class DocumentConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文档接口
|
* 文档接口
|
||||||
|
*
|
||||||
* @return httpinvoker
|
* @return httpinvoker
|
||||||
*/
|
*/
|
||||||
@Bean(name = "/document-service")
|
@Bean(name = "/document-service")
|
||||||
HttpInvokerServiceExporter wordService(DocumentService documentService) {
|
HttpInvokerServiceExporter wordService(DocumentService documentService) {
|
||||||
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
|
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
|
||||||
exporter.setService( documentService );
|
exporter.setService(documentService);
|
||||||
exporter.setServiceInterface(DocumentService.class);
|
exporter.setServiceInterface(DocumentService.class);
|
||||||
return exporter;
|
return exporter;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user