From ee223d040574367e253aac622b65c6c01c584d0d Mon Sep 17 00:00:00 2001 From: TieStone <416138794@qq.com> Date: Mon, 8 Jan 2024 19:38:47 +0800 Subject: [PATCH] add table support --- build.gradle | 1 + .../controller/api/converters/ConvertMarkdownToPdf.java | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 40dda97f..2f3dad04 100644 --- a/build.gradle +++ b/build.gradle @@ -152,6 +152,7 @@ dependencies { implementation group: 'com.google.zxing', name: 'core', version: '3.5.2' // https://mvnrepository.com/artifact/org.commonmark/commonmark implementation 'org.commonmark:commonmark:0.21.0' + implementation 'org.commonmark:commonmark-ext-gfm-tables:0.17.2' // https://mvnrepository.com/artifact/com.github.vladimir-bukhtoyarov/bucket4j-core implementation 'com.github.vladimir-bukhtoyarov:bucket4j-core:7.6.0' diff --git a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java index d0fd632d..abfcb139 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java +++ b/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertMarkdownToPdf.java @@ -1,5 +1,9 @@ package stirling.software.SPDF.controller.api.converters; +import java.util.List; + +import org.commonmark.Extension; +import org.commonmark.ext.gfm.tables.TablesExtension; import org.commonmark.node.Node; import org.commonmark.parser.Parser; import org.commonmark.renderer.html.HtmlRenderer; @@ -47,9 +51,10 @@ public class ConvertMarkdownToPdf { } // Convert Markdown to HTML using CommonMark - Parser parser = Parser.builder().build(); + List extensions = List.of(TablesExtension.create()); + Parser parser = Parser.builder().extensions(extensions).build(); Node document = parser.parse(new String(fileInput.getBytes())); - HtmlRenderer renderer = HtmlRenderer.builder().build(); + HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build(); String htmlContent = renderer.render(document); byte[] pdfBytes =