分离tl-server和server中接口的非公共方法,将公共方法wordToPdf放到BaseDocumentService中

BaseDocumentService.java中增加doc转docx、xls转xlsx的方法
BaseDocumentService.java中增加convert方法
This commit is contained in:
杨黄林
2022-12-28 18:23:55 +08:00
parent f38fd6e635
commit 820c307e89
8 changed files with 269 additions and 120 deletions

View File

@@ -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转pdfserver和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转为docxserver和tl-server下均实现
*
* @param docData doc文档流
* @return docx文档流
*/
default byte[] docToDocx(byte[] docData) {
throw new UnsupportedOperationException();
}
/**
* xls转为xlsxserver和tl-server下均实现
*
* @param xlsData xls文档流
* @return xlsx文档流
*/
default byte[] xlsToXlsx(byte[] xlsData) {
throw new UnsupportedOperationException();
}
}

View File

@@ -1,71 +1,44 @@
package com.optima.document.api;
import java.util.List;
import java.util.Map;
/**
* 文档接口
* @author Elias
* @date 2021-09-28 16:00
* 文档接口此类中用的poi2生成word文档使用docto进行格式转换
*
* @author yanghuanglin
* @since 2022/12/28
*/
public interface DocumentService {
public interface DocumentService extends BaseDocumentService {
/**
* generate word
* @param templateData word模版流
* 通过调用poi生成word仅tl-server模块下实现仅支持docx格式
*
* @param templateData word模版流仅支持docx格式
* @param dataModel 数据模型
* @return 修改后的文档流
* @return word文档流
*/
default byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
throw new UnsupportedOperationException();
}
/**
* word to pdf
* @param templateData word模版流
* 通过调用poi将word转pdf仅tl-server模块下实现仅支持docx格式
*
* @param templateData word模版流仅支持docx格式
* @param clear 是否清除占位符
* @return
* @return pdf文档流
*/
default byte[] wordToPdf(byte[] templateData, boolean clear) {
throw new UnsupportedOperationException();
}
/**
* word to image
* @param templateData word模版流
* @param targetFormat 目标格式 支持jpeg, jpg, gif, tiff or png
* @return
*/
default byte[] wordToImage(byte[] templateData, String targetFormat) {
throw new UnsupportedOperationException();
}
/**
* word转图片仅支持docx格式
*
* @param source 文档
* @param toFindText 需要替换的文本
* @param imgSource 图片
* @param width 宽度
* @param height 高度
* @return 修改后的文档
* @param source word文件流仅支持docx格式
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
* @return 图片
*/
default byte[] insertJpeg(byte[] source, String toFindText, byte[] imgSource, int width, int height){
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) {
default byte[] wordToImage(byte[] source, String targetExtension) {
throw new UnsupportedOperationException();
}
}

View File

@@ -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();
}
}

View File

@@ -5,14 +5,17 @@ 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.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 lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -21,11 +24,44 @@ import java.util.Map;
/**
* 服务接口实现
*
* @author Elias
* @date 2021-09-28 16:18
* @since 2021-09-28 16:18
*/
@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) {
return DocToPdfUtil.fieldToWord(source, infoMap);
@@ -39,39 +75,31 @@ public class DocumentServiceImpl implements DocumentService {
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 {
if (clear) {
source = DocToPdfUtil.clearPlaceholder(source);
}
long t1 = System.currentTimeMillis();
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();
return convert(source, sourceExtension, "pdf");
} catch (Exception e) {
e.printStackTrace();
}
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 {
byte[] pdfBytes = wordToPdf(source, sourceFormat, true);
byte[] pdfBytes = wordToPdf(source, sourceExtension, true);
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage bim = pdfRenderer.renderImageWithDPI(
0, 300, ImageType.RGB);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIOUtil.writeImage(bim, targetFormat, bos, 300);
ImageIOUtil.writeImage(bim, targetExtension, bos, 300);
document.close();
return bos.toByteArray();
} catch (Exception e) {
@@ -79,4 +107,12 @@ public class DocumentServiceImpl implements DocumentService {
}
return null;
}
public byte[] docToDocx(byte[] docData) {
return convert(docData, "doc", "docx");
}
public byte[] xlsToXlsx(byte[] xlsData) {
return convert(xlsData, "xls", "xlsx");
}
}

View File

@@ -1,7 +1,6 @@
package com.optima.document.server.config;
import com.optima.document.api.DocumentService;
import com.optima.document.server.api.DocumentServiceImpl;
import com.optima.document.api.LegacyDocumentService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
@@ -10,9 +9,11 @@ import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
/**
* 服务端配置
*
* @author Elias
* @date 2021-09-28 16:12
* @since 2021-09-28 16:12
*/
@SuppressWarnings("VulnerableCodeUsages")
@Configuration
@ConfigurationProperties(prefix = "document")
@Data
@@ -24,13 +25,14 @@ public class DocumentConfig {
/**
* 文档接口
*
* @return httpinvoker
*/
@Bean(name = "/document-service")
HttpInvokerServiceExporter wordService() {
HttpInvokerServiceExporter wordService(LegacyDocumentService legacyDocumentService) {
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setService( new DocumentServiceImpl() );
exporter.setServiceInterface(DocumentService.class);
exporter.setService(legacyDocumentService);
exporter.setServiceInterface(LegacyDocumentService.class);
return exporter;
}

View File

@@ -1,5 +1,5 @@
server:
port: 8090
document:
doc-to-program: D:\\optima\\docto.exe
open-office-home: D:\\OpenOffice4\\program\\soffice.exe

View File

@@ -13,30 +13,33 @@ import com.deepoove.poi.xwpf.BodyContainerFactory;
import com.optima.document.api.DocumentService;
import com.optima.document.tl.server.config.DocumentConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
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
* @date 2021-09-28 16:18
* @since 2021-09-28 16:18
*/
@Slf4j
@Component
@Service
public class DocumentServiceImpl implements DocumentService {
/**
@@ -83,7 +86,44 @@ public class DocumentServiceImpl implements DocumentService {
@Resource
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) {
long start = System.currentTimeMillis();
XWPFTemplate template = XWPFTemplate.compile(new ByteArrayInputStream(templateData), wtlConfig).render(dataModel);
@@ -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) {
try {
long start = System.currentTimeMillis();
@@ -119,52 +162,36 @@ public class DocumentServiceImpl implements DocumentService {
return null;
}
}
Path tempFilePath = Files.createTempFile(UUID.randomUUID().toString(), ".docx");
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);
}
return convert(source, "docx", "pdf", "wdFormatPDF");
} catch (Exception e) {
log.error("word to pdf error", e);
return null;
}
}
@Override
public byte[] wordToImage(byte[] source, String targetFormat) {
public byte[] wordToImage(byte[] source, String targetExtension) {
try {
byte[] pdfBytes = wordToPdf(source, true);
long start = System.currentTimeMillis();
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage bim = pdfRenderer.renderImageWithDPI(
0, 300, ImageType.RGB);
BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIOUtil.writeImage(bim, targetFormat, bos, 300);
ImageIOUtil.writeImage(bim, targetExtension, bos, 300);
document.close();
log.info("word to image=======consuming{} milliseconds", System.currentTimeMillis() - start);
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
log.error("word to image error", e);
}
return null;
}
public byte[] docToDocx(byte[] docData) {
return convert(docData, "doc", "docx", "wdFormatDocumentDefault");
}
public byte[] xlsToXlsx(byte[] xlsData) {
return convert(xlsData, "xls", "xlsx", "xlWorkbookDefault");
}
}

View File

@@ -1,24 +1,19 @@
package com.optima.document.tl.server.config;
import com.optima.document.api.DocumentService;
import com.optima.document.tl.server.api.DocumentServiceImpl;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
* @date 2021-09-28 16:12
* @since 2021-09-28 16:12
*/
@SuppressWarnings("VulnerableCodeUsages")
@Configuration
@ConfigurationProperties(prefix = "document")
@Data
@@ -28,6 +23,7 @@ public class DocumentConfig {
/**
* 文档接口
*
* @return httpinvoker
*/
@Bean(name = "/document-service")