分离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

@@ -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