将wordToImage做为公共方法放到BaseDocumentService中
代码调整
This commit is contained in:
@@ -8,45 +8,59 @@ public interface BaseDocumentService {
|
|||||||
/**
|
/**
|
||||||
* 格式转换,server和tl-server下均实现
|
* 格式转换,server和tl-server下均实现
|
||||||
*
|
*
|
||||||
* @param sourceData 源文件流,tl-server下仅支持docx格式
|
* @param source 源文件流,tl-server下仅支持docx格式
|
||||||
* @param sourceExtension 源文件后缀名,不包含"."
|
* @param sourceExtension 源文件后缀名,不包含"."
|
||||||
* @param targetExtension 目标文件后缀名,不包含"."
|
* @param targetExtension 目标文件后缀名,不包含"."
|
||||||
* @param targetFormat 目标文件格式
|
* @param targetFormat 目标文件格式,需与目标文件后缀名匹配,server下此参数无效。,tl-server下参考: <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdsaveformat">word格式</a>
|
||||||
|
* 或 <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.xlfileformat">excel格式</a>
|
||||||
|
* 或 <a href="https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.saveas">powerpoint格式</a>
|
||||||
* @return 转换后的文件流
|
* @return 转换后的文件流
|
||||||
*/
|
*/
|
||||||
default byte[] convert(byte[] sourceData, String sourceExtension, String targetExtension, String targetFormat) {
|
default byte[] convert(byte[] source, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通word转pdf,server和tl-server下均实现
|
* word转为pdf,server和tl-server下均实现
|
||||||
*
|
*
|
||||||
* @param source 源文件流,tl-server下仅支持docx格式
|
* @param source word文件流,tl-server下仅支持docx格式
|
||||||
* @param sourceFormat 源文件后缀名,不包含".",tl-server下可忽略
|
* @param sourceFormat 源文件后缀名,不包含".",tl-server下此参数无效
|
||||||
* @param clear 是否清除占位符
|
* @param clear 是否清除占位符,如果为true,则在tl-server下源文件只支持docx格式
|
||||||
* @return pdf文档流
|
* @return pdf文档流
|
||||||
*/
|
*/
|
||||||
default byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
default byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过pdfbox将word转图片
|
||||||
|
*
|
||||||
|
* @param source word文件流,tl-server下仅支持docx格式
|
||||||
|
* @param sourceExtension 源文件后缀名,不包含".",server下此参数无效
|
||||||
|
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
|
||||||
|
* @return 图片流
|
||||||
|
*/
|
||||||
|
default byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* doc转为docx,server和tl-server下均实现
|
* doc转为docx,server和tl-server下均实现
|
||||||
*
|
*
|
||||||
* @param docData doc文档流
|
* @param source doc文档流
|
||||||
* @return docx文档流
|
* @return docx文档流
|
||||||
*/
|
*/
|
||||||
default byte[] docToDocx(byte[] docData) {
|
default byte[] docToDocx(byte[] source) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xls转为xlsx,server和tl-server下均实现
|
* xls转为xlsx,server和tl-server下均实现
|
||||||
*
|
*
|
||||||
* @param xlsData xls文档流
|
* @param source xls文档流
|
||||||
* @return xlsx文档流
|
* @return xlsx文档流
|
||||||
*/
|
*/
|
||||||
default byte[] xlsToXlsx(byte[] xlsData) {
|
default byte[] xlsToXlsx(byte[] source) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,27 +12,27 @@ public interface DocumentService extends BaseDocumentService {
|
|||||||
/**
|
/**
|
||||||
* 通过调用poi生成word,仅tl-server模块下实现,仅支持docx格式
|
* 通过调用poi生成word,仅tl-server模块下实现,仅支持docx格式
|
||||||
*
|
*
|
||||||
* @param templateData word模版流,仅支持docx格式
|
* @param sourceTemplate word模版流,仅支持docx格式
|
||||||
* @param dataModel 数据模型
|
* @param dataModel 数据模型
|
||||||
* @return word文档流
|
* @return word文档流
|
||||||
*/
|
*/
|
||||||
default byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
|
default byte[] generateWord(byte[] sourceTemplate, Map<String, Object> dataModel) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过调用poi将word转pdf,仅tl-server模块下实现,仅支持docx格式
|
* 通过调用poi将word转pdf,仅tl-server模块下实现,如果clear为true,则仅支持docx格式
|
||||||
*
|
*
|
||||||
* @param templateData word模版流,仅支持docx格式
|
* @param source word模版流,仅支持docx格式
|
||||||
* @param clear 是否清除占位符
|
* @param clear 是否清除占位符
|
||||||
* @return pdf文档流
|
* @return pdf文档流
|
||||||
*/
|
*/
|
||||||
default byte[] wordToPdf(byte[] templateData, boolean clear) {
|
default byte[] wordToPdf(byte[] source, boolean clear) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* word转图片,仅支持docx格式
|
* 通过pdfbox将word转图片,仅支持docx格式
|
||||||
*
|
*
|
||||||
* @param source word文件流,仅支持docx格式
|
* @param source word文件流,仅支持docx格式
|
||||||
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
|
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
|
||||||
|
|||||||
@@ -10,18 +10,6 @@ import java.util.Map;
|
|||||||
* @since 2021-09-28 16:00
|
* @since 2021-09-28 16:00
|
||||||
*/
|
*/
|
||||||
public interface LegacyDocumentService extends BaseDocumentService {
|
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模块下实现
|
* 通过jacob向文档中插入图片,仅server模块下实现
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ public class LegacyDocumentServiceImpl implements LegacyDocumentService {
|
|||||||
*/
|
*/
|
||||||
private byte[] convert(byte[] source, String sourceExtension, String targetExtension) {
|
private byte[] convert(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
try {
|
try {
|
||||||
|
sourceExtension = sourceExtension.replace(".","");
|
||||||
|
targetExtension = targetExtension.replace(".","");
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
String command = "%s -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
|
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()));
|
Process p = Runtime.getRuntime().exec(String.format(command, documentConfig.getOpenOfficeHome()));
|
||||||
@@ -55,7 +57,7 @@ public class LegacyDocumentServiceImpl implements LegacyDocumentService {
|
|||||||
converter.convert(new ByteArrayInputStream(source), formatRegistry.getFormatByFileExtension(sourceExtension), bos, formatRegistry.getFormatByFileExtension(targetExtension));
|
converter.convert(new ByteArrayInputStream(source), formatRegistry.getFormatByFileExtension(sourceExtension), bos, formatRegistry.getFormatByFileExtension(targetExtension));
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
p.destroy();
|
p.destroy();
|
||||||
log.info("{} to {} take {} milliseconds", sourceExtension, targetExtension, System.currentTimeMillis() - start);
|
log.info("openoffice convert {} to {} take time in millis:{}", sourceExtension, targetExtension, System.currentTimeMillis() - start);
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -87,8 +89,8 @@ public class LegacyDocumentServiceImpl implements LegacyDocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] convert(byte[] sourceData, String sourceExtension, String targetExtension, String targetFormat) {
|
public byte[] convert(byte[] source, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
return convert(sourceData, sourceExtension, targetExtension);
|
return convert(source, sourceExtension, targetExtension);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
public byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
@@ -108,11 +110,11 @@ public class LegacyDocumentServiceImpl implements LegacyDocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] docToDocx(byte[] docData) {
|
public byte[] docToDocx(byte[] source) {
|
||||||
return convert(docData, "doc", "docx");
|
return convert(source, "doc", "docx");
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] xlsToXlsx(byte[] xlsData) {
|
public byte[] xlsToXlsx(byte[] source) {
|
||||||
return convert(xlsData, "xls", "xlsx");
|
return convert(source, "xls", "xlsx");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import java.util.UUID;
|
|||||||
* @author Elias
|
* @author Elias
|
||||||
* @since 2021-09-28 16:18
|
* @since 2021-09-28 16:18
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class DocumentServiceImpl implements DocumentService {
|
public class DocumentServiceImpl implements DocumentService {
|
||||||
@@ -86,15 +87,13 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
@Resource
|
@Resource
|
||||||
private DocumentConfig documentConfig;
|
private DocumentConfig documentConfig;
|
||||||
|
|
||||||
public byte[] convert(byte[] sourceData, String sourceExtension, String targetExtension, String targetFormat) {
|
public byte[] convert(byte[] source, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
try {
|
try {
|
||||||
if (!sourceExtension.contains("."))
|
sourceExtension = sourceExtension.replace(".", "");
|
||||||
sourceExtension = "." + sourceExtension;
|
targetExtension = targetExtension.replace(".", "");
|
||||||
if (!targetExtension.contains("."))
|
Path sourcePath = Files.createTempFile(UUID.randomUUID().toString(), "." + sourceExtension);
|
||||||
targetExtension = "." + targetExtension;
|
Path targetPath = Files.createTempFile(UUID.randomUUID().toString(), "." + targetExtension);
|
||||||
Path sourcePath = Files.createTempFile(UUID.randomUUID().toString(), sourceExtension);
|
Files.write(sourcePath, source);
|
||||||
Path targetPath = Files.createTempFile(UUID.randomUUID().toString(), targetExtension);
|
|
||||||
Files.write(sourcePath, sourceData);
|
|
||||||
try {
|
try {
|
||||||
long begin = System.currentTimeMillis();
|
long begin = System.currentTimeMillis();
|
||||||
String command;
|
String command;
|
||||||
@@ -109,7 +108,7 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
Process p = Runtime.getRuntime().exec(command);
|
Process p = Runtime.getRuntime().exec(command);
|
||||||
p.waitFor();
|
p.waitFor();
|
||||||
long end = System.currentTimeMillis();
|
long end = System.currentTimeMillis();
|
||||||
log.info("docto {} to {} take time in millis:{}", sourceExtension, targetExtension, (end - begin));
|
log.info("docto convert {} to {} take time in millis:{}", sourceExtension, targetExtension, (end - begin));
|
||||||
return Files.readAllBytes(targetPath);
|
return Files.readAllBytes(targetPath);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -124,12 +123,12 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] generateWord(byte[] templateData, Map<String, Object> dataModel) {
|
public byte[] generateWord(byte[] sourceTemplate, 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(sourceTemplate), 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 take time in millis:{}", System.currentTimeMillis() - start);
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("word generate error", e);
|
log.error("word generate error", e);
|
||||||
@@ -179,7 +178,7 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
ImageIOUtil.writeImage(bim, targetExtension, 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 take time in millis:{}", System.currentTimeMillis() - start);
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("word to image error", e);
|
log.error("word to image error", e);
|
||||||
@@ -187,11 +186,15 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] docToDocx(byte[] docData) {
|
public byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
return convert(docData, "doc", "docx", "wdFormatDocumentDefault");
|
return wordToImage(source, targetExtension);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] xlsToXlsx(byte[] xlsData) {
|
public byte[] docToDocx(byte[] source) {
|
||||||
return convert(xlsData, "xls", "xlsx", "xlWorkbookDefault");
|
return convert(source, "doc", "docx", "wdFormatDocumentDefault");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] xlsToXlsx(byte[] source) {
|
||||||
|
return convert(source, "xls", "xlsx", "xlWorkbookDefault");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user