把文书模板语法配置、自定义列表标签配置、自定义字符串列表元素拼接分割符提取成docker镜像的环境变量
This commit is contained in:
18
README.md
18
README.md
@@ -89,6 +89,14 @@ services:
|
|||||||
- DOCUMENT_SERVER_PORT=9004
|
- DOCUMENT_SERVER_PORT=9004
|
||||||
- PORT_NUMBERS=2001,2002,2003
|
- PORT_NUMBERS=2001,2002,2003
|
||||||
- MAX_TASKS_PER_PROCESS=100
|
- MAX_TASKS_PER_PROCESS=100
|
||||||
|
# 传递环境变量时,$ 是特殊符号,需要 $$ 来表示
|
||||||
|
# linux 命令中,$ 也是特殊符号,需要在前面增加 \ 表示转义特殊符号
|
||||||
|
# 如果需要传递 ${ ,则需要写成 \$${ 。其首先被 docker 解析为 \${ ,\${ 作为linux命令的一部分,$转义后,相当于字符串 ${
|
||||||
|
# 如果不是类似 ${ ,如 {{ ,则无需转义
|
||||||
|
- GRAMER_PREFIX="\$${"
|
||||||
|
- GRAMER_SUFFIX="}"
|
||||||
|
- GRAMER_CUSTOMIZE_LIST="%"
|
||||||
|
- GRAMER_CUSTOMIZE_LIST_STRING_DELIMITING=","
|
||||||
ports:
|
ports:
|
||||||
# 用于通过http访问libreoffice
|
# 用于通过http访问libreoffice
|
||||||
- 3000:3000
|
- 3000:3000
|
||||||
@@ -119,6 +127,16 @@ services:
|
|||||||
server:
|
server:
|
||||||
port: ${DOCUMENT_SERVER_PORT:9004}
|
port: ${DOCUMENT_SERVER_PORT:9004}
|
||||||
|
|
||||||
|
document:
|
||||||
|
gramer:
|
||||||
|
default:
|
||||||
|
prefix: '${'
|
||||||
|
suffix: '}'
|
||||||
|
prefix: ${GRAMER_PREFIX:${document.gramer.default.prefix}}
|
||||||
|
suffix: ${GRAMER_SUFFIX:${document.gramer.default.suffix}}
|
||||||
|
customize-list-tag: ${GRAMER_CUSTOMIZE_LIST:%}
|
||||||
|
customize-list-tag-string-delimiting: ${GRAMER_CUSTOMIZE_LIST_STRING_DELIMITING:,}
|
||||||
|
|
||||||
jodconverter:
|
jodconverter:
|
||||||
local:
|
local:
|
||||||
# 启动本地转换
|
# 启动本地转换
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.util.Map;
|
|||||||
* @since 2022/12/28
|
* @since 2022/12/28
|
||||||
*/
|
*/
|
||||||
public interface DocumentService {
|
public interface DocumentService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式转换
|
* 格式转换
|
||||||
*
|
*
|
||||||
@@ -79,4 +80,24 @@ public interface DocumentService {
|
|||||||
default byte[] xlsToXlsx(byte[] source) {
|
default byte[] xlsToXlsx(byte[] source) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模板语法配置
|
||||||
|
* <ul>
|
||||||
|
* <li>
|
||||||
|
* 模板语法参考:<a href="https://deepoove.com/poi-tl/">POI-TL语法</a>
|
||||||
|
* </li>
|
||||||
|
* <li>
|
||||||
|
* 这里的配置会将原来的 {{ 替换为{@link Gramer#getPrefix()}, }} 替换为{@link Gramer#getSuffix()}
|
||||||
|
* </li>
|
||||||
|
* <li>
|
||||||
|
* 增加以{@link Gramer#getCustomizeListTag()}开头的列表对象处理插件,其字符串类型列表分隔符为{@link Gramer#getCustomizeListTagStringDelimiting()}
|
||||||
|
* </li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @return 模板语法配置
|
||||||
|
*/
|
||||||
|
default Gramer gramer() {
|
||||||
|
return new Gramer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.optima.document.api;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文书模板中的语法标签
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class Gramer {
|
||||||
|
/// 模板语法开始字符串
|
||||||
|
private String prefix = "${";
|
||||||
|
/// 模板语法结束字符串
|
||||||
|
private String suffix = "}";
|
||||||
|
/// 自定义列表对象标签
|
||||||
|
private Character customizeList = '%';
|
||||||
|
/// 自定义列表对象标签处理字符串列表时的分隔符
|
||||||
|
private String customizeListStringDelimiting = ",";
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"name": "document.gramer.prefix",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "模板语法开始字符串. 默认为 '${'.",
|
||||||
|
"defaultValue": "${",
|
||||||
|
"sourceType": "com.optima.document.api.Gramer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "document.gramer.suffix",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "模板语法结束字符串. 默认为 '}'.",
|
||||||
|
"defaultValue": "}",
|
||||||
|
"sourceType": "com.optima.document.api.Gramer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "document.gramer.customize-list",
|
||||||
|
"type": "java.lang.Character",
|
||||||
|
"description": "自定义列表对象标签. 默认为 '%'. 只能为单个字符.",
|
||||||
|
"defaultValue": "%",
|
||||||
|
"sourceType": "com.optima.document.api.Gramer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "document.gramer.customize-list-string-delimiting",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "自定义列表对象标签处理字符串列表时的分隔符. 默认为 '、'.",
|
||||||
|
"defaultValue": "、",
|
||||||
|
"sourceType": "com.optima.document.api.Gramer"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -31,6 +31,10 @@ ENV CUSTOM_HTTPS_PORT=3001
|
|||||||
ENV DOCUMENT_SERVER_PORT=9004
|
ENV DOCUMENT_SERVER_PORT=9004
|
||||||
ENV PORT_NUMBERS=2002
|
ENV PORT_NUMBERS=2002
|
||||||
ENV MAX_TASKS_PER_PROCESS=200
|
ENV MAX_TASKS_PER_PROCESS=200
|
||||||
|
ENV GRAMER_PREFIX="\\\${"
|
||||||
|
ENV GRAMER_SUFFIX="}"
|
||||||
|
ENV GRAMER_CUSTOMIZE_LIST="%"
|
||||||
|
ENV GRAMER_CUSTOMIZE_LIST_STRING_DELIMITING=","
|
||||||
|
|
||||||
EXPOSE 9004
|
EXPOSE 9004
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ mkdir -p app
|
|||||||
|
|
||||||
# 构建jar
|
# 构建jar
|
||||||
echo 构建jar
|
echo 构建jar
|
||||||
mvn -f ../document-server/jodconverter-document-server/pom.xml clean package -Dmaven.test.skip=true
|
mvn -f ../pom.xml clean package -Dmaven.test.skip=true
|
||||||
echo jar构建完成
|
echo jar构建完成
|
||||||
|
|
||||||
# 复制所需文件
|
# 复制所需文件
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ exec java -jar /app/application.jar \
|
|||||||
--server.port=${DOCUMENT_SERVER_PORT} \
|
--server.port=${DOCUMENT_SERVER_PORT} \
|
||||||
--spring.config.location=/app/application.yml \
|
--spring.config.location=/app/application.yml \
|
||||||
--jodconverter.local.port-numbers=${PORT_NUMBERS} \
|
--jodconverter.local.port-numbers=${PORT_NUMBERS} \
|
||||||
--jodconverter.local.max-tasks-per-process=${MAX_TASKS_PER_PROCESS}
|
--jodconverter.local.max-tasks-per-process=${MAX_TASKS_PER_PROCESS} \
|
||||||
|
--document.gramer.prefix="${GRAMER_PREFIX}" \
|
||||||
|
--document.gramer.suffix="${GRAMER_SUFFIX}" \
|
||||||
|
--document.gramer.customize-list="${GRAMER_CUSTOMIZE_LIST}" \
|
||||||
|
--document.gramer.customize-list-string-delimiting="${GRAMER_CUSTOMIZE_LIST_STRING_DELIMITING}"
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# 设置可执行权限
|
# 设置可执行权限
|
||||||
|
|||||||
@@ -2,22 +2,17 @@ package com.optima.document.server.api;
|
|||||||
|
|
||||||
import com.deepoove.poi.XWPFTemplate;
|
import com.deepoove.poi.XWPFTemplate;
|
||||||
import com.deepoove.poi.config.Configure;
|
import com.deepoove.poi.config.Configure;
|
||||||
import com.deepoove.poi.data.PictureRenderData;
|
|
||||||
import com.deepoove.poi.data.TextRenderData;
|
|
||||||
import com.deepoove.poi.policy.ListRenderPolicy;
|
|
||||||
import com.deepoove.poi.policy.PictureRenderPolicy;
|
|
||||||
import com.deepoove.poi.render.RenderContext;
|
|
||||||
import com.deepoove.poi.template.run.RunTemplate;
|
import com.deepoove.poi.template.run.RunTemplate;
|
||||||
import com.deepoove.poi.xwpf.BodyContainer;
|
import com.deepoove.poi.xwpf.BodyContainer;
|
||||||
import com.deepoove.poi.xwpf.BodyContainerFactory;
|
import com.deepoove.poi.xwpf.BodyContainerFactory;
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.DocumentService;
|
||||||
|
import com.optima.document.api.Gramer;
|
||||||
import com.optima.document.server.config.DocumentConfig;
|
import com.optima.document.server.config.DocumentConfig;
|
||||||
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.apache.poi.xwpf.usermodel.XWPFRun;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -27,8 +22,6 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
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.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -38,53 +31,13 @@ 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 {
|
||||||
@Resource
|
@Resource
|
||||||
private DocumentConfig documentConfig;
|
private DocumentConfig documentConfig;
|
||||||
|
@Resource
|
||||||
/**
|
private Configure wtlConfig;
|
||||||
* word模版引擎配置
|
|
||||||
*/
|
|
||||||
Configure wtlConfig = Configure.builder().buildGramer("${", "}")
|
|
||||||
.setValidErrorHandler(new Configure.DiscardHandler())
|
|
||||||
.addPlugin('%', new ListRenderPolicy() {
|
|
||||||
@Override
|
|
||||||
public void doRender(RenderContext<List<Object>> context) throws Exception {
|
|
||||||
XWPFRun run = context.getRun();
|
|
||||||
List<?> dataList = context.getData();
|
|
||||||
Iterator<?> var5 = dataList.iterator();
|
|
||||||
while (var5.hasNext()) {
|
|
||||||
Object data = var5.next();
|
|
||||||
if (data instanceof TextRenderData) {
|
|
||||||
run.setText(((TextRenderData) data).getText());
|
|
||||||
if (var5.hasNext()) {
|
|
||||||
run.setText(",");
|
|
||||||
}
|
|
||||||
} else if (data instanceof PictureRenderData) {
|
|
||||||
PictureRenderPolicy.Helper.renderPicture(run, (PictureRenderData) data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
.setRenderDataComputeFactory(envModel ->
|
|
||||||
el -> {
|
|
||||||
Object data = envModel.getRoot();
|
|
||||||
if ("#this".equals(el)) {
|
|
||||||
return data;
|
|
||||||
} else if (data instanceof Map) {
|
|
||||||
Map dataMap = ((Map) data);
|
|
||||||
if (dataMap.containsKey(el)) {
|
|
||||||
return dataMap.get(el);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
})
|
|
||||||
.build();
|
|
||||||
|
|
||||||
public byte[] convert(byte[] source, String sourceExtension, String targetExtension, String targetFormat) {
|
public byte[] convert(byte[] source, String sourceExtension, String targetExtension, String targetFormat) {
|
||||||
try {
|
try {
|
||||||
@@ -188,4 +141,8 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
public byte[] xlsToXlsx(byte[] source) {
|
public byte[] xlsToXlsx(byte[] source) {
|
||||||
return convert(source, "xls", "xlsx", "xlWorkbookDefault");
|
return convert(source, "xls", "xlsx", "xlWorkbookDefault");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Gramer gramer() {
|
||||||
|
return documentConfig.getGramer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,25 @@
|
|||||||
package com.optima.document.server.config;
|
package com.optima.document.server.config;
|
||||||
|
|
||||||
|
import com.deepoove.poi.config.Configure;
|
||||||
|
import com.deepoove.poi.data.PictureRenderData;
|
||||||
|
import com.deepoove.poi.data.TextRenderData;
|
||||||
|
import com.deepoove.poi.policy.ListRenderPolicy;
|
||||||
|
import com.deepoove.poi.policy.PictureRenderPolicy;
|
||||||
|
import com.deepoove.poi.render.RenderContext;
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.DocumentService;
|
||||||
|
import com.optima.document.api.Gramer;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||||
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.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端配置
|
* 服务端配置
|
||||||
*
|
*
|
||||||
@@ -20,6 +33,60 @@ public class DocumentConfig {
|
|||||||
|
|
||||||
private String docToProgram;
|
private String docToProgram;
|
||||||
|
|
||||||
|
@NestedConfigurationProperty
|
||||||
|
private Gramer gramer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* word模版引擎配置
|
||||||
|
*/
|
||||||
|
@Bean(name = "wtlConfig")
|
||||||
|
public Configure wtlConfig() {
|
||||||
|
return Configure.builder().buildGramer(gramer.getPrefix(), gramer.getSuffix())
|
||||||
|
.setValidErrorHandler(new Configure.DiscardHandler())
|
||||||
|
.addPlugin(gramer.getCustomizeList(), new ListRenderPolicy() {
|
||||||
|
@Override
|
||||||
|
public void doRender(RenderContext<List<Object>> context) throws Exception {
|
||||||
|
XWPFRun run = context.getRun();
|
||||||
|
List<?> dataList = context.getData();
|
||||||
|
Iterator<?> iterator = dataList.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Object data = iterator.next();
|
||||||
|
if (data instanceof String) {
|
||||||
|
// 纯文本类型
|
||||||
|
run.setText(data.toString());
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
run.setText(gramer.getCustomizeListStringDelimiting());
|
||||||
|
}
|
||||||
|
} else if (data instanceof TextRenderData) {
|
||||||
|
// poi的文本类型
|
||||||
|
run.setText(((TextRenderData) data).getText());
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
run.setText(gramer.getCustomizeListStringDelimiting());
|
||||||
|
}
|
||||||
|
} else if (data instanceof PictureRenderData) {
|
||||||
|
// poi的图片类型
|
||||||
|
PictureRenderPolicy.Helper.renderPicture(run, (PictureRenderData) data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
.setRenderDataComputeFactory(envModel ->
|
||||||
|
el -> {
|
||||||
|
Object data = envModel.getRoot();
|
||||||
|
if ("#this".equals(el)) {
|
||||||
|
return data;
|
||||||
|
} else if (data instanceof Map) {
|
||||||
|
@SuppressWarnings("rawtypes") Map dataMap = ((Map) data);
|
||||||
|
if (dataMap.containsKey(el)) {
|
||||||
|
return dataMap.get(el);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文档接口
|
* 文档接口
|
||||||
*
|
*
|
||||||
@@ -33,5 +100,4 @@ public class DocumentConfig {
|
|||||||
exporter.setServiceInterface(DocumentService.class);
|
exporter.setServiceInterface(DocumentService.class);
|
||||||
return exporter;
|
return exporter;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,4 +2,13 @@ server:
|
|||||||
port: 9004
|
port: 9004
|
||||||
|
|
||||||
document:
|
document:
|
||||||
|
gramer:
|
||||||
|
default:
|
||||||
|
prefix: '${'
|
||||||
|
suffix: '}'
|
||||||
|
prefix: ${GRAMER_PREFIX:${document.gramer.default.prefix}}
|
||||||
|
suffix: ${GRAMER_SUFFIX:${document.gramer.default.suffix}}
|
||||||
|
customize-list: ${GRAMER_CUSTOMIZE_LIST:%}
|
||||||
|
customize-list-string-delimiting: ${GRAMER_CUSTOMIZE_LIST_STRING_DELIMITING:,}
|
||||||
|
|
||||||
doc-to-program: C:\\DocumentServer\\docto.exe
|
doc-to-program: C:\\DocumentServer\\docto.exe
|
||||||
@@ -2,21 +2,17 @@ package com.optima.document.server.api;
|
|||||||
|
|
||||||
import com.deepoove.poi.XWPFTemplate;
|
import com.deepoove.poi.XWPFTemplate;
|
||||||
import com.deepoove.poi.config.Configure;
|
import com.deepoove.poi.config.Configure;
|
||||||
import com.deepoove.poi.data.PictureRenderData;
|
|
||||||
import com.deepoove.poi.data.TextRenderData;
|
|
||||||
import com.deepoove.poi.policy.ListRenderPolicy;
|
|
||||||
import com.deepoove.poi.policy.PictureRenderPolicy;
|
|
||||||
import com.deepoove.poi.render.RenderContext;
|
|
||||||
import com.deepoove.poi.template.run.RunTemplate;
|
import com.deepoove.poi.template.run.RunTemplate;
|
||||||
import com.deepoove.poi.xwpf.BodyContainer;
|
import com.deepoove.poi.xwpf.BodyContainer;
|
||||||
import com.deepoove.poi.xwpf.BodyContainerFactory;
|
import com.deepoove.poi.xwpf.BodyContainerFactory;
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.DocumentService;
|
||||||
|
import com.optima.document.api.Gramer;
|
||||||
|
import com.optima.document.server.config.DocumentConfig;
|
||||||
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.apache.poi.xwpf.usermodel.XWPFRun;
|
|
||||||
import org.jodconverter.core.DocumentConverter;
|
import org.jodconverter.core.DocumentConverter;
|
||||||
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
|
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
|
||||||
import org.jodconverter.core.document.DocumentFormat;
|
import org.jodconverter.core.document.DocumentFormat;
|
||||||
@@ -26,8 +22,6 @@ 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.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,47 +35,10 @@ import java.util.Map;
|
|||||||
public class DocumentServiceImpl implements DocumentService {
|
public class DocumentServiceImpl implements DocumentService {
|
||||||
@Resource
|
@Resource
|
||||||
private DocumentConverter documentConverter;
|
private DocumentConverter documentConverter;
|
||||||
|
@Resource
|
||||||
/**
|
private DocumentConfig documentConfig;
|
||||||
* word模版引擎配置
|
@Resource
|
||||||
*/
|
private Configure wtlConfig;
|
||||||
private static final Configure wtlConfig = Configure.builder().buildGramer("${", "}")
|
|
||||||
.setValidErrorHandler(new Configure.DiscardHandler())
|
|
||||||
.addPlugin('%', new ListRenderPolicy() {
|
|
||||||
@Override
|
|
||||||
public void doRender(RenderContext<List<Object>> context) throws Exception {
|
|
||||||
XWPFRun run = context.getRun();
|
|
||||||
List<?> dataList = context.getData();
|
|
||||||
Iterator<?> var5 = dataList.iterator();
|
|
||||||
while (var5.hasNext()) {
|
|
||||||
Object data = var5.next();
|
|
||||||
if (data instanceof TextRenderData) {
|
|
||||||
run.setText(((TextRenderData) data).getText());
|
|
||||||
if (var5.hasNext()) {
|
|
||||||
run.setText(",");
|
|
||||||
}
|
|
||||||
} else if (data instanceof PictureRenderData) {
|
|
||||||
PictureRenderPolicy.Helper.renderPicture(run, (PictureRenderData) data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
.setRenderDataComputeFactory(envModel ->
|
|
||||||
el -> {
|
|
||||||
Object data = envModel.getRoot();
|
|
||||||
if ("#this".equals(el)) {
|
|
||||||
return data;
|
|
||||||
} else if (data instanceof Map) {
|
|
||||||
Map dataMap = ((Map) data);
|
|
||||||
if (dataMap.containsKey(el)) {
|
|
||||||
return dataMap.get(el);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
})
|
|
||||||
.build();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件格式转换
|
* 文件格式转换
|
||||||
@@ -185,4 +142,7 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return convert(source, "xls", "xlsx");
|
return convert(source, "xls", "xlsx");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Gramer gramer() {
|
||||||
|
return documentConfig.getGramer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,91 @@
|
|||||||
package com.optima.document.server.config;
|
package com.optima.document.server.config;
|
||||||
|
|
||||||
|
import com.deepoove.poi.config.Configure;
|
||||||
|
import com.deepoove.poi.data.PictureRenderData;
|
||||||
|
import com.deepoove.poi.data.TextRenderData;
|
||||||
|
import com.deepoove.poi.policy.ListRenderPolicy;
|
||||||
|
import com.deepoove.poi.policy.PictureRenderPolicy;
|
||||||
|
import com.deepoove.poi.render.RenderContext;
|
||||||
import com.optima.document.api.DocumentService;
|
import com.optima.document.api.DocumentService;
|
||||||
|
import com.optima.document.api.Gramer;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||||
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.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端配置
|
* 服务端配置
|
||||||
*
|
*
|
||||||
* @author Elias
|
* @author Elias
|
||||||
* @since 2021-09-28 16:12
|
* @since 2021-09-28 16:12
|
||||||
*/
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "document")
|
||||||
public class DocumentConfig {
|
public class DocumentConfig {
|
||||||
|
@NestedConfigurationProperty
|
||||||
|
private Gramer gramer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* word模版引擎配置
|
||||||
|
*/
|
||||||
|
@Bean(name = "wtlConfig")
|
||||||
|
public Configure wtlConfig() {
|
||||||
|
return Configure.builder().buildGramer(gramer.getPrefix(), gramer.getSuffix())
|
||||||
|
.setValidErrorHandler(new Configure.DiscardHandler())
|
||||||
|
.addPlugin(gramer.getCustomizeList(), new ListRenderPolicy() {
|
||||||
|
@Override
|
||||||
|
public void doRender(RenderContext<List<Object>> context) throws Exception {
|
||||||
|
XWPFRun run = context.getRun();
|
||||||
|
List<?> dataList = context.getData();
|
||||||
|
Iterator<?> iterator = dataList.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Object data = iterator.next();
|
||||||
|
if (data instanceof String) {
|
||||||
|
// 纯文本类型
|
||||||
|
run.setText(data.toString());
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
run.setText(gramer.getCustomizeListStringDelimiting());
|
||||||
|
}
|
||||||
|
} else if (data instanceof TextRenderData) {
|
||||||
|
// poi的文本类型
|
||||||
|
run.setText(((TextRenderData) data).getText());
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
run.setText(gramer.getCustomizeListStringDelimiting());
|
||||||
|
}
|
||||||
|
} else if (data instanceof PictureRenderData) {
|
||||||
|
// poi的图片类型
|
||||||
|
PictureRenderPolicy.Helper.renderPicture(run, (PictureRenderData) data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
.setRenderDataComputeFactory(envModel ->
|
||||||
|
el -> {
|
||||||
|
Object data = envModel.getRoot();
|
||||||
|
if ("#this".equals(el)) {
|
||||||
|
return data;
|
||||||
|
} else if (data instanceof Map) {
|
||||||
|
@SuppressWarnings("rawtypes") Map dataMap = ((Map) data);
|
||||||
|
if (dataMap.containsKey(el)) {
|
||||||
|
return dataMap.get(el);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文档接口
|
* 文档接口
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,6 +1,16 @@
|
|||||||
server:
|
server:
|
||||||
port: ${DOCUMENT_SERVER_PORT:9004}
|
port: ${DOCUMENT_SERVER_PORT:9004}
|
||||||
|
|
||||||
|
document:
|
||||||
|
gramer:
|
||||||
|
default:
|
||||||
|
prefix: '${'
|
||||||
|
suffix: '}'
|
||||||
|
prefix: ${GRAMER_PREFIX:${document.gramer.default.prefix}}
|
||||||
|
suffix: ${GRAMER_SUFFIX:${document.gramer.default.suffix}}
|
||||||
|
customize-list: ${GRAMER_CUSTOMIZE_LIST:%}
|
||||||
|
customize-list-string-delimiting: ${GRAMER_CUSTOMIZE_LIST_STRING_DELIMITING:,}
|
||||||
|
|
||||||
jodconverter:
|
jodconverter:
|
||||||
local:
|
local:
|
||||||
# 启动本地转换
|
# 启动本地转换
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -35,6 +37,11 @@ public class DocumentConverter {
|
|||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("callerName", "张三");
|
params.put("callerName", "张三");
|
||||||
|
|
||||||
|
List<String> departmentList = new ArrayList<>();
|
||||||
|
departmentList.add("处置部门1");
|
||||||
|
departmentList.add("处置部门2");
|
||||||
|
params.put("departmentList", departmentList);
|
||||||
|
|
||||||
// 生成word,并转为pdf
|
// 生成word,并转为pdf
|
||||||
byte[] generatedWord = documentService.generateWord(Files.readAllBytes(sourceFile.toPath()), params);
|
byte[] generatedWord = documentService.generateWord(Files.readAllBytes(sourceFile.toPath()), params);
|
||||||
byte[] wordedToPdf = documentService.wordToPdf(generatedWord, true);
|
byte[] wordedToPdf = documentService.wordToPdf(generatedWord, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user