|
@@ -0,0 +1,170 @@
|
|
|
|
+package com.ruoyi.project.common.controller;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+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.deepoove.poi.XWPFTemplate;
|
|
|
|
+import com.ruoyi.common.utils.file.FileUploadUtils;
|
|
|
|
+import com.ruoyi.common.utils.poi.ExcelUtil;
|
|
|
|
+import com.ruoyi.framework.aspectj.lang.annotation.Log;
|
|
|
|
+import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
|
|
|
|
+import com.ruoyi.framework.config.RuoYiConfig;
|
|
|
|
+import com.ruoyi.framework.web.controller.BaseController;
|
|
|
|
+import com.ruoyi.framework.web.domain.AjaxResult;
|
|
|
|
+import com.ruoyi.framework.web.page.TableDataInfo;
|
|
|
|
+import com.ruoyi.project.common.domain.TCommonfile;
|
|
|
|
+import com.ruoyi.project.common.service.ITCommonfileService;
|
|
|
|
+import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
|
|
|
|
+import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
|
|
|
|
+import io.jsonwebtoken.lang.Assert;
|
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 通用附件Controller
|
|
|
|
+ *
|
|
|
|
+ * @author ruoyi
|
|
|
|
+ * @date 2020-12-11
|
|
|
|
+ */
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("/common/word")
|
|
|
|
+public class WordController extends BaseController
|
|
|
|
+{
|
|
|
|
+ /**
|
|
|
|
+ * @author lsc
|
|
|
|
+ * @param templatePath word模板文件路径
|
|
|
|
+ * @param fileDir 生成的文件存放地址
|
|
|
|
+ * @param fileName 生成的文件名
|
|
|
|
+ * @param paramMap 参数集合
|
|
|
|
+ * @return 返回word生成的路径
|
|
|
|
+ */
|
|
|
|
+ public String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap) {
|
|
|
|
+ Assert.notNull(templatePath, "word模板文件路径不能为空");
|
|
|
|
+ Assert.notNull(fileDir, "生成的文件存放地址不能为空");
|
|
|
|
+ Assert.notNull(fileName, "生成的文件名不能为空");
|
|
|
|
+ File dir = new File(fileDir);
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ logger.info("目录不存在,创建文件夹{}!", fileDir);
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ String filePath = fileDir +"\\"+ fileName;
|
|
|
|
+ // 读取模板渲染参数
|
|
|
|
+ InputStream is = getClass().getClassLoader().getResourceAsStream(templatePath);
|
|
|
|
+ XWPFTemplate template = XWPFTemplate.compile(is).render(paramMap);
|
|
|
|
+ //将word转成pdf
|
|
|
|
+// PdfOptions options = PdfOptions.create();
|
|
|
|
+// try (OutputStream outPDF = Files.newOutputStream(Paths.get("D:/ticket/demo/tes2.pdf"))) {
|
|
|
|
+// PdfConverter.getInstance().convert(template.getXWPFDocument(), outPDF, options);
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+// }
|
|
|
|
+ try {
|
|
|
|
+ // 将模板参数写入路径
|
|
|
|
+ template.writeToFile(filePath);
|
|
|
|
+ template.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("生成word异常{}", e.getMessage());
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return filePath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 通用附件上传
|
|
|
|
+ */
|
|
|
|
+ @Log(title = "通用附件上传", businessType = BusinessType.UPDATE)
|
|
|
|
+ @GetMapping("/createWord")
|
|
|
|
+ public AjaxResult word() throws IOException
|
|
|
|
+ {
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
+ // 渲染文本
|
|
|
|
+ params.put("no","10210120102");
|
|
|
|
+// params.put("apply_man","知识追寻者");
|
|
|
|
+
|
|
|
|
+ // 模板路径
|
|
|
|
+ String templatePath = "static/word/ticket.docx";
|
|
|
|
+ // 生成word的路径
|
|
|
|
+ String fileDir = "D:/ticket/demo";
|
|
|
|
+ // 生成word的文件
|
|
|
|
+ String fileName = "zszxz.docx";
|
|
|
|
+ String wordPath = createWord(templatePath, fileDir, fileName, params);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ //读取word文档
|
|
|
|
+// XWPFDocument document = null;
|
|
|
|
+// try (InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/zszxz.docx"))) {
|
|
|
|
+// document = new XWPFDocument(in);
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+// }
|
|
|
|
+ //将word转成pdf
|
|
|
|
+// PdfOptions options = PdfOptions.create();
|
|
|
|
+// try (OutputStream outPDF = Files.newOutputStream(Paths.get("D:/ticket/demo/tes2.pdf"))) {
|
|
|
|
+// PdfConverter.getInstance().convert(document, outPDF, options);
|
|
|
|
+// } catch (IOException e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+// }
|
|
|
|
+ String srcPath = "D:/ticket/demo/zszxz.docx";
|
|
|
|
+ String desPath = "D:/ticket/demo/tes2.pdf";
|
|
|
|
+ Word2Pdf(srcPath, desPath);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ System.out.println("生成文档路径:" + wordPath);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 将word格式的文件转换为pdf格式
|
|
|
|
+ public void Word2Pdf(String srcPath, String desPath) throws IOException {
|
|
|
|
+ // 源文件目录
|
|
|
|
+ File inputFile = new File(srcPath);
|
|
|
|
+ if (!inputFile.exists()) {
|
|
|
|
+ System.out.println("源文件不存在!");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ // 输出文件目录
|
|
|
|
+ File outputFile = new File(desPath);
|
|
|
|
+ if (!outputFile.getParentFile().exists()) {
|
|
|
|
+ outputFile.getParentFile().exists();
|
|
|
|
+ }
|
|
|
|
+ // 调用openoffice服务线程
|
|
|
|
+ String command = "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
|
|
|
|
+ Process p = Runtime.getRuntime().exec(command);
|
|
|
|
+
|
|
|
|
+ // 连接openoffice服务
|
|
|
|
+ OpenOfficeConnection connection = new SocketOpenOfficeConnection(
|
|
|
|
+ "127.0.0.1", 8100);
|
|
|
|
+ connection.connect();
|
|
|
|
+
|
|
|
|
+ // 转换word到pdf
|
|
|
|
+ DocumentConverter converter = new OpenOfficeDocumentConverter(
|
|
|
|
+ connection);
|
|
|
|
+ converter.convert(inputFile, outputFile);
|
|
|
|
+
|
|
|
|
+ // 关闭连接
|
|
|
|
+ connection.disconnect();
|
|
|
|
+
|
|
|
|
+ // 关闭进程
|
|
|
|
+ p.destroy();
|
|
|
|
+ System.out.println("转换完成!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|