|
@@ -1,9 +1,6 @@
|
|
|
package com.ruoyi.project.common.controller;
|
|
|
|
|
|
-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.constant.Constants;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
@@ -14,7 +11,13 @@ 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.jodconverter.core.DocumentConverter;
|
|
|
+import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
@@ -35,6 +38,9 @@ import java.util.Map;
|
|
|
@RequestMapping("/common/word")
|
|
|
public class WordController extends BaseController
|
|
|
{
|
|
|
+ @Resource
|
|
|
+ private DocumentConverter documentConverter;
|
|
|
+
|
|
|
/**
|
|
|
* @author lsc
|
|
|
* @param templatePath word模板文件路径
|
|
@@ -75,42 +81,59 @@ public class WordController extends BaseController
|
|
|
@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.doc";
|
|
|
- String wordPath = createWord(templatePath, fileDir, fileName, params);
|
|
|
-
|
|
|
+ System.out.println("使用流的方式转换PDF!");
|
|
|
+ //使用流的方式转换PDF
|
|
|
try {
|
|
|
- //读取word文档
|
|
|
- XWPFDocument document = null;
|
|
|
- try (InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/sems.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/html.pdf"))) {
|
|
|
- PdfConverter.getInstance().convert(document, outPDF, options);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- String srcPath = "D:/ticket/demo/sems.doc";
|
|
|
- String desPath = "D:/ticket/demo/openoffice.pdf";
|
|
|
- Word2Pdf(srcPath, desPath);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/sems.docx"));
|
|
|
+ OutputStream outPDF = Files.newOutputStream(Paths.get("D:/ticket/demo/html.pdf"));
|
|
|
+ documentConverter.convert(in)
|
|
|
+ .as(DefaultDocumentFormatRegistry.DOCX)
|
|
|
+ .to(outPDF)
|
|
|
+ .as(DefaultDocumentFormatRegistry.PDF)
|
|
|
+ .execute();
|
|
|
+ System.out.println("转换完成!");
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println("转换出错!");
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- System.out.println("生成文档路径:" + wordPath);
|
|
|
+
|
|
|
+// 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.doc";
|
|
|
+// String wordPath = createWord(templatePath, fileDir, fileName, params);
|
|
|
+//
|
|
|
+// try {
|
|
|
+// //读取word文档
|
|
|
+// XWPFDocument document = null;
|
|
|
+// try (InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/sems.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/html.pdf"))) {
|
|
|
+// PdfConverter.getInstance().convert(document, outPDF, options);
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// String srcPath = "D:/ticket/demo/sems.doc";
|
|
|
+// String desPath = "D:/ticket/demo/openoffice.pdf";
|
|
|
+// Word2Pdf(srcPath, desPath);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// System.out.println("生成文档路径:" + wordPath);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -132,17 +155,17 @@ public class WordController extends BaseController
|
|
|
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();
|
|
|
+// 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();
|