ly 3 lat temu
rodzic
commit
1733b432e0

+ 15 - 0
master/pom.xml

@@ -302,6 +302,21 @@
 			<artifactId>poi</artifactId>
 			<version>4.1.2</version>
 		</dependency>
+		<dependency>
+			<groupId>com.deepoove</groupId>
+			<artifactId>poi-tl</artifactId>
+			<version>1.10.2</version>
+		</dependency>
+		<dependency>
+			<groupId>fr.opensagres.xdocreport</groupId>
+			<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
+			<version>2.0.2</version>
+		</dependency>
+		<!-- https://mvnrepository.com/artifact/com.artofsolving/jodconverter-maven-plugin -->
+		<dependency>
+			<groupId>com.artofsolving</groupId>
+			<artifactId>jodconverter-maven-plugin</artifactId>
+			<version>2.2.1</version></dependency>
 		<!--<dependency>
 			<groupId>org.apache.poi</groupId>
 			<artifactId>poi-ooxml</artifactId>

+ 1 - 0
master/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -108,6 +108,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 .antMatchers("/profile/**").anonymous()
                 .antMatchers("/common/download/exportDevList").anonymous()
                 .antMatchers("/common/download**").anonymous()
+                .antMatchers("/common/word/createWord").anonymous()
                 .antMatchers("/common/download/resource**").anonymous()
                 .antMatchers("/swagger-ui.html").anonymous()
                 .antMatchers("/swagger-resources/**").anonymous()

+ 170 - 0
master/src/main/java/com/ruoyi/project/common/controller/WordController.java

@@ -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("转换完成!");
+    }
+
+}

+ 1 - 1
master/src/main/resources/application.yml

@@ -9,7 +9,7 @@ ruoyi:
   # 实例演示开关
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /data/cpmsfile/uploadPath)
-  profile: /data/cpmsfile/uploadPath
+  profile: /data/jobticketFile/uploadPath
   # 获取ip地址开关
   addressEnabled: false
   # 验证码类型 math 数组计算 char 字符验证

BIN
master/src/main/resources/static/word/ticket.docx