From 68aae14c5c33b525effd4fe18a6ce14ea6ec90ae Mon Sep 17 00:00:00 2001 From: 354798ggg <1074825718@qq.com> Date: 星期二, 16 五月 2023 10:52:18 +0800 Subject: [PATCH] 打印变更word下载 --- src/main/java/com/product/print/service/ide/IPrintRealizeService.java | 2 src/main/java/com/product/print/service/PrintRealizeService.java | 157 ++++++++++++++++++++++++++++++++------- src/main/java/com/product/print/controller/PrintRealizeController.java | 28 +++++++ 3 files changed, 159 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/product/print/controller/PrintRealizeController.java b/src/main/java/com/product/print/controller/PrintRealizeController.java index f841411..6a7e876 100644 --- a/src/main/java/com/product/print/controller/PrintRealizeController.java +++ b/src/main/java/com/product/print/controller/PrintRealizeController.java @@ -58,5 +58,33 @@ return error(CmnCode.GET_PRINT_CONFIG_LIST_FAIL, e); } } + + @PostMapping("/printWord/{version}") + @ApiVersion(1) + public String printWord(HttpServletRequest request, HttpServletResponse response) { + try { + FieldSetEntity fse = null; + Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); + if (bean != null) { + RequestParameterEntity reqp = (RequestParameterEntity) bean; + fse = reqp.getFormData(); + } + if (bean == null || fse == null) { + return this.error(CmnCode.SYSTEM_FORM_NODATA); + } +// if (!CmnConst.TABLE_PRINT_CONFIG.equals(fse.getTableName())) { +// return error(CmnCode.SYSTEM_TABLE_NODATA); +// } + if (StringUtils.isEmpty(CmnConst.PRINT_TEMP)) { + return error(CmnCode.SYSTEM_FORM_COUNT); + } + printRealizeService.printWord(fse, response); + return OK(); + } catch (BaseException e) { + return error(e); + } catch (Exception e) { + return error(CmnCode.GET_PRINT_CONFIG_LIST_FAIL, e); + } + } } diff --git a/src/main/java/com/product/print/service/PrintRealizeService.java b/src/main/java/com/product/print/service/PrintRealizeService.java index 483cc58..7ebb4a8 100644 --- a/src/main/java/com/product/print/service/PrintRealizeService.java +++ b/src/main/java/com/product/print/service/PrintRealizeService.java @@ -13,11 +13,16 @@ import com.product.print.config.CmnCode; import com.product.print.config.CmnConst; import com.product.print.service.ide.IPrintRealizeService; +import com.product.util.BaseUtil; +import com.product.util.SystemParamReplace; +import com.product.util.config.SystemParamSet; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.*; +import java.net.URLEncoder; import java.util.*; import java.util.stream.Collectors; @@ -34,7 +39,7 @@ @Autowired FileManagerService fileManagerService; - +/* public static void main(String[] args) { Map<String, Object> t = new HashMap<>(); Integer a = 1; @@ -44,35 +49,131 @@ Map<String, String> cc = (Map<String, String>) (Map) t; System.out.println(cc); } +*/ +/* + public static void main(String[] args) throws Exception { + Map<String, Object> map = new HashMap<>(); + List<Map<String, String>> subMapList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + Map<String, String> subMap = new HashMap<>(); + subMap.put("a1", "鍝佸悕鈥斺��" + (i + 1)); + subMap.put("a2", "鍗曚綅鈥斺��" + (i + 1)); + subMap.put("a3", "鏁伴噺鈥斺��" + (i + 1)); + subMap.put("a4", "鐢ㄩ�斺�斺��" + (i + 1)); + subMapList.add(subMap); + } + map.put("BGYPLYB_SUB", subMapList); + HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();//鍒涘缓涓�涓垪琛ㄧ殑瑙勫垯 + Configure config = Configure.newBuilder().customPolicy("BGYPLYB_SUB", policy).customPolicy("test_table", policy).build(); + File targetFile = new File("C:\\Users\\cheng\\Desktop\\replaceWord.docx"); + File file = new File("C:\\Users\\cheng\\Desktop\\鍔炲叕鐢ㄥ搧棰嗙敤琛�.docx"); + if (targetFile.exists()) { + targetFile.delete(); + } + targetFile.createNewFile(); + try (FileOutputStream is = new FileOutputStream(targetFile); + ) { + XWPFTemplate render = XWPFTemplate.compile(file, config).render(map); + render.write(is); + render.close(); + } + } +*/ + + @Override + public void printWord(FieldSetEntity fse, HttpServletResponse response) throws BaseException { + // 鎵撳嵃閰嶇疆uuid + String print_uuid = fse.getString("~" + CmnConst.PRINT_TEMP + "~"); + //鏌ヨ鎵撳嵃閰嶇疆 + FieldSetEntity fieldSetEntity = getBaseDao().getFieldSetEntity(CmnConst.TABLE_PRINT_CONFIG, print_uuid, false); + if (fieldSetEntity == null || StringUtils.isEmpty(fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE))) { + throw new BaseException(CmnCode.PRINT_CONFIG_NOT_EXIST.getValue(), CmnCode.PRINT_CONFIG_NOT_EXIST.getText()); + } + // 鎵撳嵃妯℃澘闄勪欢uuid + String template_uuid = fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE); + // 鑾峰彇鎵撳嵃妯℃澘 + File file = getFile(template_uuid); + // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭�� + getBaseDao().loadPromptData(fse); + // 鏂囦欢鍚嶅墠缂�閮ㄥ垎 + Object tempKey = UUID.randomUUID(); + // 鏇挎崲鍚庣殑word涓存椂璺緞 + String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx"; + File wordTemp = new File(localTempPathWord); + try { + new File(Global.getSystemConfig("temp.dir", "")).mkdirs(); + wordTemp.createNewFile(); + } catch (Exception e) { + e.printStackTrace(); + throw new BaseException(CmnCode.PRINT_CONTENT_FAIL.getValue(), CmnCode.PRINT_CONTENT_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); + } + //灏嗘ā鍜屽綋鍓峟se鐨剉alues鏀惧叆杩涜鏇挎崲 + try (FileOutputStream is = new FileOutputStream(wordTemp); + OutputStream out = response.getOutputStream()) { + Map<String, DataTableEntity> subData = fse.getSubData(); -// public static void main(String[] args) throws Exception { -// Map<String, Object> map = new HashMap<>(); -// List<Map<String, String>> subMapList = new ArrayList<>(); -// for (int i = 0; i < 5; i++) { -// Map<String, String> subMap = new HashMap<>(); -// subMap.put("a1", "鍝佸悕鈥斺��" + (i + 1)); -// subMap.put("a2", "鍗曚綅鈥斺��" + (i + 1)); -// subMap.put("a3", "鏁伴噺鈥斺��" + (i + 1)); -// subMap.put("a4", "鐢ㄩ�斺�斺��" + (i + 1)); -// subMapList.add(subMap); -// } -// map.put("BGYPLYB_SUB", subMapList); -// HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();//鍒涘缓涓�涓垪琛ㄧ殑瑙勫垯 -// Configure config = Configure.newBuilder().customPolicy("BGYPLYB_SUB", policy).customPolicy("test_table", policy).build(); -// File targetFile = new File("C:\\Users\\cheng\\Desktop\\replaceWord.docx"); -// File file = new File("C:\\Users\\cheng\\Desktop\\鍔炲叕鐢ㄥ搧棰嗙敤琛�.docx"); -// if (targetFile.exists()) { -// targetFile.delete(); -// } -// targetFile.createNewFile(); -// try (FileOutputStream is = new FileOutputStream(targetFile); -// ) { -// XWPFTemplate render = XWPFTemplate.compile(file, config).render(map); -// render.write(is); -// render.close(); -// } -// } + Configure.ConfigureBuilder configureBuilder = null; + if (subData != null && subData.size() > 0) { + for (Map.Entry<String, DataTableEntity> vv : + subData.entrySet()) { + getBaseDao().loadPromptData(vv.getValue()); + if (!DataTableEntity.isEmpty(vv.getValue())) { + List<Map> collect = vv.getValue().getData().stream().map(item -> (Map<String, String>) ((Map) item.getValues())).collect(Collectors.toList()); + fse.setValue(vv.getKey(), collect); + } else { + fse.setValue(vv.getKey(), new ArrayList<>()); + } + if (configureBuilder == null) { + configureBuilder = Configure.newBuilder(); + } + configureBuilder.customPolicy(vv.getKey(), new HackLoopTableRenderPolicy()); + } + } + XWPFTemplate render = XWPFTemplate.compile(file, configureBuilder == null ? Configure.createDefault() : configureBuilder.build()).render(fse.getValues()); + render.write(is); + render.close(); + response.setContentType("multipart/form-data"); + response.setHeader("Access-Control-Allow-Origin", "*"); // 鍏佽鎵�鏈� + //璁剧疆鍝嶅簲 + response.setContentType("application/octet-stream;charset=UTF-8"); + // 灏嗗搷搴斿ご涓殑Content-Disposition鏆撮湶鍑烘潵锛屼笉鐒跺墠绔幏鍙栦笉鍒� + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + + String fileName = null; + if(!BaseUtil.strIsNull(fieldSetEntity.getString("print_file_name"))) { + fileName = SystemParamReplace.replaceParams(fieldSetEntity.getString("print_file_name"), fse); + }else { + fileName = fieldSetEntity.getString("print_name"); + } + fileName+=".docx"; + + // 鍦ㄥ搷搴斿ご涓殑Content-Disposition閲岃缃枃浠跺悕绉� + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); + InputStream isPdf = new FileInputStream(wordTemp); + int len; + byte[] b = new byte[1024]; + // 杈撳嚭 + while ((len = isPdf.read(b)) > 0) { + out.write(b, 0, len); + } + isPdf.close(); + } catch (Exception e) { + e.printStackTrace(); + throw new BaseException(CmnCode.PRINT_CONTENT_FAIL.getValue(), CmnCode.PRINT_CONTENT_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); + } finally { + //鍒犻櫎word涓存椂鏂囦欢 + if (wordTemp.exists()) { + System.out.println(wordTemp.getPath()); +// wordTemp.delete(); + } + if (file != null) { + file.delete(); + } + } + } + + @Override public void print(FieldSetEntity fse, HttpServletResponse response) throws BaseException { // 鎵撳嵃閰嶇疆uuid diff --git a/src/main/java/com/product/print/service/ide/IPrintRealizeService.java b/src/main/java/com/product/print/service/ide/IPrintRealizeService.java index 2e6d5ba..ccea719 100644 --- a/src/main/java/com/product/print/service/ide/IPrintRealizeService.java +++ b/src/main/java/com/product/print/service/ide/IPrintRealizeService.java @@ -14,6 +14,8 @@ */ public interface IPrintRealizeService { + void printWord(FieldSetEntity fse, HttpServletResponse response) throws BaseException; + void print(FieldSetEntity fse, HttpServletResponse response) throws BaseException; } -- Gitblit v1.9.2