diff --git a/build.gradle b/build.gradle index 40dda97f..d9f62736 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.21.0' // 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..fe590459 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,7 +1,14 @@ package stirling.software.SPDF.controller.api.converters; +import java.util.List; +import java.util.Map; + +import org.commonmark.Extension; +import org.commonmark.ext.gfm.tables.TableBlock; +import org.commonmark.ext.gfm.tables.TablesExtension; import org.commonmark.node.Node; import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.AttributeProvider; import org.commonmark.renderer.html.HtmlRenderer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -47,9 +54,16 @@ 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() + .attributeProviderFactory(context -> new TableAttributeProvider()) + .extensions(extensions) + .build(); + String htmlContent = renderer.render(document); byte[] pdfBytes = @@ -62,3 +76,12 @@ public class ConvertMarkdownToPdf { return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); } } + +class TableAttributeProvider implements AttributeProvider { + @Override + public void setAttributes(Node node, String tagName, Map attributes) { + if (node instanceof TableBlock) { + attributes.put("class", "table table-striped"); + } + } +} diff --git a/src/main/resources/static/3rdPartyLicenses.json b/src/main/resources/static/3rdPartyLicenses.json index 25e8e91b..88263b1d 100644 --- a/src/main/resources/static/3rdPartyLicenses.json +++ b/src/main/resources/static/3rdPartyLicenses.json @@ -356,22 +356,22 @@ }, { "moduleName": "org.apache.pdfbox:fontbox", - "moduleUrl": "http://pdfbox.apache.org/", - "moduleVersion": "2.0.29", + "moduleUrl": "https://pdfbox.apache.org", + "moduleVersion": "2.0.30", "moduleLicense": "Apache License, Version 2.0", "moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt" }, { "moduleName": "org.apache.pdfbox:pdfbox", - "moduleUrl": "http://pdfbox.apache.org", - "moduleVersion": "2.0.29", + "moduleUrl": "https://pdfbox.apache.org", + "moduleVersion": "2.0.30", "moduleLicense": "Apache License, Version 2.0", "moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt" }, { "moduleName": "org.apache.pdfbox:xmpbox", - "moduleUrl": "http://pdfbox.apache.org", - "moduleVersion": "2.0.29", + "moduleUrl": "https://pdfbox.apache.org", + "moduleVersion": "2.0.30", "moduleLicense": "Apache License, Version 2.0", "moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt" },