Merge pull request 'beta' (#8) from beta into master

Reviewed-on: crm/crm-print#8
This commit is contained in:
luobai 2024-01-12 17:38:55 +08:00
commit 4a3d56407c
4 changed files with 136 additions and 9 deletions

View File

@ -1,7 +1,5 @@
FROM openjdk:8u181-jre-alpine
ENV LANG en_US.UTF-8
RUN mkdir -p /usr/share/fonts/chinese
COPY fonts/* /usr/share/fonts/chinese/
RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories &&\
apk add tzdata &&\
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone &&\
@ -12,6 +10,7 @@ RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk
apk add msttcorefonts-installer &&\
apk add fontconfig &&\
apk --update add unzip &&\
mkdir -p /usr/share/fonts/chinese &&\
cd /tmp &&\
wget http://kkfileview.keking.cn/fonts.zip &&\
unzip -d /usr/share/fonts/chinese fonts.zip &&\
@ -19,14 +18,9 @@ RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk
mkfontscale &&\
mkfontdir &&\
fc-cache -fv &&\
COPY /usr/share/fonts/korean /usr/share/fonts &&\
cd /usr/share/fonts/korean &&\
mkfontscale &&\
mkfontdir &&\
fc-cache -fv &&\
rm -rf /var/cache/*
COPY ./server/target/crm-print-server-1.0.0.jar /
WORKDIR /
EXPOSE 9089
CMD ["java","-Xms1000m","-Xmx1000m","-jar","crm-print-server-1.0.0.jar"]
CMD ["java", "-Xms3072m", "-jar","crm-print-server-1.0.0.jar"]

View File

@ -9,6 +9,8 @@ import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -26,7 +28,7 @@ public class KkfileConfig {
*/
private static final long DEFAULT_TEMPLATE_MAX_SIZE = 10 * 1024 * 1024;
private static final List<String> ALLOW_UPLOAD_TYPE = Collections.singletonList("docx");
private static final List<String> ALLOW_UPLOAD_TYPE =Arrays.asList("docx","xlsx");
/**
* 预览地址

View File

@ -1,5 +1,7 @@
package cn.keking.service;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.keking.config.ConfigConstants;
import cn.keking.config.KkfileConfig;
@ -7,10 +9,12 @@ import cn.keking.exception.KkFileException;
import cn.keking.service.impl.OtherFilePreviewImpl;
import cn.keking.utils.Consts;
import cn.keking.utils.WebUtils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
@ -278,6 +282,37 @@ public class ContractService {
}
}
}
public File downloadExcel(String templateName, Map<String, Object> dataMap) throws Exception {
OutputStream os = null;
try {
File outFile = new File(Consts.uploadAbsDir());
if (!outFile.exists()) {
outFile.mkdirs();
}
String templatePath = Consts.getTemplatePath();
String abTemplatePath = templatePath + File.separator + templateName + ".xlsx";
//判断模板是否存在
File file = new File(abTemplatePath);
if(!file.exists()){
throw new KkFileException("请先上传模板!");
}
String excelFile = "/"+ UUID.randomUUID().toString().replace("-", "") + ".xlsx";
//通过填参 生产文件 并放到 upload 文件夹中
String path = Consts.uploadAbsDir();
String pathFile = path + excelFile;
os = new FileOutputStream(pathFile);
TemplateExportParams params = new TemplateExportParams(
abTemplatePath);
Workbook workbook = ExcelExportUtil.exportExcel(params, dataMap);
workbook.write(os);
return new File(pathFile);
} finally {
if (os != null) {
os.close();
}
}
}
@ -323,4 +358,39 @@ public class ContractService {
}
}
}
public String excel(String templateName, Map<String, Object> dataMap) throws Exception {
OutputStream os = null;
try {
File outFile = new File(Consts.uploadAbsDir());
if (!outFile.exists()) {
outFile.mkdirs();
}
String templatePath = Consts.getTemplatePath();
String abTemplatePath = templatePath + File.separator + templateName + ".xlsx";
//判断模板是否存在
File file = new File(abTemplatePath);
if (!file.exists()) {
throw new KkFileException("请先上传模板!");
}
TemplateExportParams params = new TemplateExportParams(
abTemplatePath);
Workbook workbook = ExcelExportUtil.exportExcel(params, dataMap);
String docxFile = "/"+ UUID.randomUUID().toString().replace("-", "") + ".xlsx";
//通过填参 生产文件 并放到 upload 文件夹中
String path = Consts.uploadAbsDir();
String pathFile = path + docxFile;
os = new FileOutputStream(pathFile);
workbook.write(os);
log.info("文件路径:{}", kkfileConfig.getLocalfileUrl() + File.separator + Consts.UPLOAD_PATH_NAME + docxFile);
String previewUrl = WebUtils.getBaseUrl() + "onlinePreview?url=" +
URLEncoder.encode(Base64.encodeBase64String((kkfileConfig.getLocalfileUrl() + File.separator + Consts.UPLOAD_PATH_NAME + docxFile).getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8.name());
return previewUrl;
} finally {
if (os != null) {
os.close();
}
}
}
}

View File

@ -61,6 +61,24 @@ public class ContractController {
return new ServiceResponse<>(null, e.getMessage(), ServiceResponse.RESULT_SERVER_ERR_CODE);
}
}
@ApiOperation("根据模板渲染excel")
@PostMapping("/excel/{templateName}")
public ServiceResponse<String> excel(
@PathVariable String templateName,
@RequestBody Map<String, Object> dataMap) {
try {
log.info("templateName:{},dataMap:{}", templateName, dataMap);
return new ServiceResponse<>(contractService.excel(templateName, dataMap), "success",
ServiceResponse.RESULT_CODE_SUCCESS_CODE);
} catch (Exception e) {
log.error("生成错误:", e);
if (e instanceof KkFileException) {
KkFileException kkFileException = (KkFileException) e;
return new ServiceResponse<>(null, kkFileException.getMessage(), ServiceResponse.RESULT_CODE_ERROR_CODE);
}
return new ServiceResponse<>(null, e.getMessage(), ServiceResponse.RESULT_SERVER_ERR_CODE);
}
}
/**
* 合同模板上传
@ -111,6 +129,49 @@ public class ContractController {
}
}
/**
* @return 下载合同excel文件
*/
@ApiParam("下载excel文件")
@PostMapping("/downloadExcel/{templateName}")
public ServiceResponse downloadExcel(
@PathVariable String templateName,
@RequestBody Map<String, Object> dataMap,
HttpServletResponse response
) {
try {
log.error(JSON.toJSONString(dataMap));
OutputStream outstream = null;
File file = contractService.downloadExcel(templateName, dataMap);
outstream = response.getOutputStream();
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
String filename =file.getName();
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
// 刷新缓冲
response.flushBuffer();
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] b = new byte[bufferedInputStream.available()];
bufferedInputStream.read(b);
outstream.write(b);
bufferedInputStream.close();
outstream.flush();
outstream.close();
return new ServiceResponse<>("success",
ServiceResponse.RESULT_CODE_SUCCESS_CODE);
} catch (Exception e) {
log.error("生成错误:", e);
if (e instanceof KkFileException) {
KkFileException kkFileException = (KkFileException) e;
return new ServiceResponse<>(null, kkFileException.getMessage(), ServiceResponse.RESULT_CODE_ERROR_CODE);
}
return new ServiceResponse<>(null, "fail", ServiceResponse.RESULT_SERVER_ERR_CODE);
}
}
/**
* @return 下载合同pdf文件
*/