|
@@ -0,0 +1,98 @@
|
|
|
|
+package com.gqy.document.service.impl;
|
|
|
|
+
|
|
|
|
+import com.gqy.common.core.domain.AjaxResult;
|
|
|
|
+import com.gqy.common.core.domain.AjaxResultCQY;
|
|
|
|
+import com.gqy.common.utils.DateUtils;
|
|
|
|
+import com.gqy.document.service.IExportService;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 导出工具
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class ExportServiceImpl implements IExportService {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public AjaxResultCQY export(String content) {
|
|
|
|
+ try {
|
|
|
|
+ // 创建临时目录
|
|
|
|
+ String tempDir = "temp_export";
|
|
|
|
+ Files.createDirectories(Paths.get(tempDir));
|
|
|
|
+
|
|
|
|
+ // 生成唯一文件名
|
|
|
|
+ String timestamp = DateUtils.dateTimeNow();
|
|
|
|
+ String fileName = "exported_document_" + timestamp + ".docx";
|
|
|
|
+ String filePath = tempDir + "/" + fileName;
|
|
|
|
+
|
|
|
|
+ // 处理HTML内容(添加标题处理)
|
|
|
|
+ String processedContent = processContent(content);
|
|
|
|
+
|
|
|
|
+ // 将处理后的HTML内容写入临时文件
|
|
|
|
+ String htmlPath = tempDir + "/input.html";
|
|
|
|
+ Files.write(Paths.get(htmlPath), processedContent.getBytes());
|
|
|
|
+
|
|
|
|
+ // 调用Pandoc转换(需要配置Pandoc路径)
|
|
|
|
+ String pandocPath = System.getProperty("os.name").toLowerCase().contains("win")
|
|
|
|
+ ? "D:\\pandoc-3.1.1\\pandoc.exe"
|
|
|
|
+ : "/usr/bin/pandoc";
|
|
|
|
+
|
|
|
|
+ ProcessBuilder pb = new ProcessBuilder(
|
|
|
|
+ pandocPath,
|
|
|
|
+ "-f", "html",
|
|
|
|
+ "-t", "docx",
|
|
|
|
+ "--toc",
|
|
|
|
+ "--toc-depth=6",
|
|
|
|
+ "--number-sections",
|
|
|
|
+ "-o", filePath,
|
|
|
|
+ htmlPath
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ Process process = pb.start();
|
|
|
|
+ int exitCode = process.waitFor();
|
|
|
|
+
|
|
|
|
+ if (exitCode == 0) {
|
|
|
|
+ // 上传到OSS
|
|
|
|
+// String ossUrl = ossService.uploadFile(filePath);
|
|
|
|
+ String ossUrl = "";
|
|
|
|
+ // 获取文件大小
|
|
|
|
+ long fileSize = Files.size(Paths.get(filePath));
|
|
|
|
+
|
|
|
|
+// // 保存记录
|
|
|
|
+// ExportRecord record = new ExportRecord();
|
|
|
|
+// record.setFile_name(fileName);
|
|
|
|
+// record.setFile_path(ossUrl);
|
|
|
|
+// record.setFile_size(fileSize);
|
|
|
|
+// record.setContent(content);
|
|
|
|
+// record.setCreate_time(new Date());
|
|
|
|
+// record.setStatus(5);
|
|
|
|
+// exportRecordMapper.insertExportRecord(record);
|
|
|
|
+
|
|
|
|
+ return AjaxResultCQY.success()
|
|
|
|
+ .put("file_name", fileName)
|
|
|
|
+ .put("file_path", ossUrl)
|
|
|
|
+ .put("file_size", fileSize);
|
|
|
|
+ } else {
|
|
|
|
+ return AjaxResultCQY.error("文档转换失败");
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return AjaxResultCQY.error("导出失败:" + e.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+ // 清理临时文件
|
|
|
|
+// FileUtils.deleteIfExists(Paths.get("temp_export").toFile());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String processContent(String content) {
|
|
|
|
+ // 处理标题格式,将A{{Directory1}}等转换为对应的HTML标题
|
|
|
|
+ return content.replaceAll(
|
|
|
|
+ "([A-Z](?:\\.\\d+)*)\\{\\{Directory(\\d+)\\}\\}",
|
|
|
|
+ "<h$2>$1</h$2>"
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|