Commit e8ea5053 authored by alex yao's avatar alex yao

feat:内容导出

parent 7eeb97f0
...@@ -212,13 +212,13 @@ ...@@ -212,13 +212,13 @@
</exclusions> </exclusions>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>4.1.2</version> <version>4.1.2</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId> <artifactId>poi-ooxml-schemas</artifactId>
...@@ -352,8 +352,6 @@ ...@@ -352,8 +352,6 @@
<artifactId>flexmark-all</artifactId> <artifactId>flexmark-all</artifactId>
<version>0.62.2</version> <version>0.62.2</version>
</dependency> </dependency>
<!-- Apache POI for generating Word documents -->
</dependencies> </dependencies>
...@@ -417,5 +415,10 @@ ...@@ -417,5 +415,10 @@
<name>Nexus aliyun</name> <name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository> </repository>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories> </repositories>
</project> </project>
package cn.com.poc.expose.dto;
/**
* @author alex.yao
* @date 2025/3/7
*/
public class ContentReportDto {
private String content;
private String reportType;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getReportType() {
return reportType;
}
public void setReportType(String reportType) {
this.reportType = reportType;
}
}
package cn.com.poc.expose.dto;
/**
* @author alex.yao
* @date 2025/3/7
*/
public class MarkdownToWordDto {
private String markdown;
public String getMarkdown() {
return markdown;
}
public void setMarkdown(String markdown) {
this.markdown = markdown;
}
}
package cn.com.poc.expose.rest; package cn.com.poc.expose.rest;
import cn.com.poc.expose.dto.MarkdownToWordDto; import cn.com.poc.expose.dto.ContentReportDto;
import cn.com.yict.framemax.core.rest.BaseRest; import cn.com.yict.framemax.core.rest.BaseRest;
import cn.com.yict.framemax.web.permission.Access; import cn.com.yict.framemax.web.permission.Access;
import cn.com.yict.framemax.web.permission.Permission; import cn.com.yict.framemax.web.permission.Permission;
...@@ -13,9 +13,9 @@ import java.io.IOException; ...@@ -13,9 +13,9 @@ import java.io.IOException;
* @date 2025/3/7 * @date 2025/3/7
*/ */
@Permission(Access.Anonymous) @Permission(Access.Anonymous)
public interface Markdown2WordRest extends BaseRest { public interface ContentReportRest extends BaseRest {
String markdown2Word(@RequestBody MarkdownToWordDto dto) throws IOException; String report(@RequestBody ContentReportDto dto) throws IOException;
} }
...@@ -3,42 +3,59 @@ package cn.com.poc.expose.rest.impl; ...@@ -3,42 +3,59 @@ package cn.com.poc.expose.rest.impl;
import cn.com.poc.common.service.BosConfigService; import cn.com.poc.common.service.BosConfigService;
import cn.com.poc.common.utils.StringUtils; import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.common.utils.UUIDTool; import cn.com.poc.common.utils.UUIDTool;
import cn.com.poc.expose.dto.MarkdownToWordDto; import cn.com.poc.expose.dto.ContentReportDto;
import cn.com.poc.expose.rest.Markdown2WordRest; import cn.com.poc.expose.rest.ContentReportRest;
import cn.com.yict.framemax.core.exception.BusinessException; import cn.com.yict.framemax.core.exception.BusinessException;
import com.itextpdf.text.pdf.PdfWriter;
import com.vladsch.flexmark.html.HtmlRenderer; import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser; import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document; import com.vladsch.flexmark.util.ast.Document;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.*; import java.io.*;
import java.util.UUID; import java.nio.charset.StandardCharsets;
/** /**
* @author alex.yao * @author alex.yao
* @date 2025/3/7 * @date 2025/3/7
*/ */
@Component @Component
public class Markdown2WordRestImpl implements Markdown2WordRest { public class ContentReportRestImpl implements ContentReportRest {
@Resource @Resource
private BosConfigService bosConfigService; private BosConfigService bosConfigService;
@Override @Override
public String markdown2Word(MarkdownToWordDto dto) throws IOException { public String report(ContentReportDto dto) throws IOException {
String markdown = dto.getMarkdown(); String markdown = dto.getContent();
if (StringUtils.isBlank(markdown)) { if (StringUtils.isBlank(markdown)) {
throw new BusinessException("markdown is blank"); throw new BusinessException("markdown is blank");
} }
String reportType = dto.getReportType();
File file = File.createTempFile(UUIDTool.getUUID(), "." + reportType);
String result = "";
if ("docx".equals(reportType) || "doc".equals(reportType)) {
String htmlContent = convertMarkdownToHtml(markdown); String htmlContent = convertMarkdownToHtml(markdown);
return convertHtmlToWord(htmlContent); result = convertHtmlToWord(htmlContent);
} else if ("html".equals(reportType)) {
String htmlContent = convertMarkdownToHtml(markdown);
result = bosConfigService.uploadFileByByteArray2Oss(htmlContent.getBytes(), UUIDTool.getUUID(), reportType);
} else if ("md".equals(reportType)) {
result = bosConfigService.uploadFileByByteArray2Oss(markdown.getBytes(), UUIDTool.getUUID(), reportType);
} else {
throw new BusinessException("reportType is not supported");
}
file.delete();
return result;
} }
private String convertMarkdownToHtml(String markdown) { private String convertMarkdownToHtml(String markdown) {
...@@ -50,10 +67,6 @@ public class Markdown2WordRestImpl implements Markdown2WordRest { ...@@ -50,10 +67,6 @@ public class Markdown2WordRestImpl implements Markdown2WordRest {
private String convertHtmlToWord(String html) throws IOException { private String convertHtmlToWord(String html) throws IOException {
File file = File.createTempFile(UUIDTool.getUUID(), ".docx"); File file = File.createTempFile(UUIDTool.getUUID(), ".docx");
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(html);
FileOutputStream outputStream = new FileOutputStream(file); FileOutputStream outputStream = new FileOutputStream(file);
ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes());//将字节数组包装到流中 ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes());//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem(); POIFSFileSystem poifs = new POIFSFileSystem();
......
...@@ -3,13 +3,11 @@ package cn.com.poc.knowledge; ...@@ -3,13 +3,11 @@ package cn.com.poc.knowledge;
import cn.com.poc.knowledge.aggregate.KnowledgeService; import cn.com.poc.knowledge.aggregate.KnowledgeService;
import cn.com.yict.framemax.core.spring.SingleContextInitializer; import cn.com.yict.framemax.core.spring.SingleContextInitializer;
import com.vladsch.flexmark.html.HtmlRenderer; import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document; import com.vladsch.flexmark.util.ast.Document;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -17,10 +15,11 @@ import org.mockito.MockitoAnnotations; ...@@ -17,10 +15,11 @@ import org.mockito.MockitoAnnotations;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import com.vladsch.flexmark.parser.Parser;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.*; import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -49,10 +48,7 @@ public class KnowledgeServiceTest { ...@@ -49,10 +48,7 @@ public class KnowledgeServiceTest {
// @Test // @Test
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException, InvalidFormatException {
// Document doc = new Document("Input.md");
// doc.save("Output.docx");
String markdownContent = readMarkdownFile("C:\\Users\\52747\\Desktop\\1.md"); String markdownContent = readMarkdownFile("C:\\Users\\52747\\Desktop\\1.md");
String htmlContent = convertMarkdownToHtml(markdownContent); String htmlContent = convertMarkdownToHtml(markdownContent);
convertHtmlToWord(htmlContent, "C:\\Users\\52747\\Desktop\\1.docx"); convertHtmlToWord(htmlContent, "C:\\Users\\52747\\Desktop\\1.docx");
...@@ -75,17 +71,11 @@ public class KnowledgeServiceTest { ...@@ -75,17 +71,11 @@ public class KnowledgeServiceTest {
} }
private static void convertHtmlToWord(String html, String outputPath) throws IOException { private static void convertHtmlToWord(String html, String outputPath) throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(html);
FileOutputStream outputStream = new FileOutputStream(outputPath); FileOutputStream outputStream = new FileOutputStream(outputPath);
ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes());//将字节数组包装到流中 ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes());//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem(); POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot(); DirectoryEntry directory = poifs.getRoot();
directory.createDocument("WordDocument", bais); directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(outputStream); poifs.writeFilesystem(outputStream);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment