mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-10-25 11:17:28 +02:00 
			
		
		
		
	add table support in md2pdf transport
This commit is contained in:
		
							parent
							
								
									ee223d0405
								
							
						
					
					
						commit
						ab9e7bbb8c
					
				| @ -152,7 +152,7 @@ dependencies { | |||||||
|     implementation group: 'com.google.zxing', name: 'core', version: '3.5.2' |     implementation group: 'com.google.zxing', name: 'core', version: '3.5.2' | ||||||
|     // https://mvnrepository.com/artifact/org.commonmark/commonmark |     // https://mvnrepository.com/artifact/org.commonmark/commonmark | ||||||
|     implementation 'org.commonmark:commonmark:0.21.0' |     implementation 'org.commonmark:commonmark:0.21.0' | ||||||
|     implementation 'org.commonmark:commonmark-ext-gfm-tables:0.17.2' |     implementation 'org.commonmark:commonmark-ext-gfm-tables:0.21.0' | ||||||
|     // https://mvnrepository.com/artifact/com.github.vladimir-bukhtoyarov/bucket4j-core |     // https://mvnrepository.com/artifact/com.github.vladimir-bukhtoyarov/bucket4j-core | ||||||
|     implementation 'com.github.vladimir-bukhtoyarov:bucket4j-core:7.6.0' |     implementation 'com.github.vladimir-bukhtoyarov:bucket4j-core:7.6.0' | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,11 +1,14 @@ | |||||||
| package stirling.software.SPDF.controller.api.converters; | package stirling.software.SPDF.controller.api.converters; | ||||||
| 
 | 
 | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import org.commonmark.Extension; | import org.commonmark.Extension; | ||||||
|  | import org.commonmark.ext.gfm.tables.TableBlock; | ||||||
| import org.commonmark.ext.gfm.tables.TablesExtension; | import org.commonmark.ext.gfm.tables.TablesExtension; | ||||||
| import org.commonmark.node.Node; | import org.commonmark.node.Node; | ||||||
| import org.commonmark.parser.Parser; | import org.commonmark.parser.Parser; | ||||||
|  | import org.commonmark.renderer.html.AttributeProvider; | ||||||
| import org.commonmark.renderer.html.HtmlRenderer; | import org.commonmark.renderer.html.HtmlRenderer; | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.beans.factory.annotation.Qualifier; | import org.springframework.beans.factory.annotation.Qualifier; | ||||||
| @ -53,9 +56,16 @@ public class ConvertMarkdownToPdf { | |||||||
|         // Convert Markdown to HTML using CommonMark |         // Convert Markdown to HTML using CommonMark | ||||||
|         List<Extension> extensions = List.of(TablesExtension.create()); |         List<Extension> extensions = List.of(TablesExtension.create()); | ||||||
|         Parser parser = Parser.builder().extensions(extensions).build(); |         Parser parser = Parser.builder().extensions(extensions).build(); | ||||||
|  | 
 | ||||||
|         Node document = parser.parse(new String(fileInput.getBytes())); |         Node document = parser.parse(new String(fileInput.getBytes())); | ||||||
|         HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build(); |         HtmlRenderer renderer = | ||||||
|  |                 HtmlRenderer.builder() | ||||||
|  |                         .attributeProviderFactory(context -> new TableAttributeProvider()) | ||||||
|  |                         .extensions(extensions) | ||||||
|  |                         .build(); | ||||||
|  | 
 | ||||||
|         String htmlContent = renderer.render(document); |         String htmlContent = renderer.render(document); | ||||||
|  |         System.out.println(htmlContent); | ||||||
| 
 | 
 | ||||||
|         byte[] pdfBytes = |         byte[] pdfBytes = | ||||||
|                 FileToPdf.convertHtmlToPdf( |                 FileToPdf.convertHtmlToPdf( | ||||||
| @ -67,3 +77,12 @@ public class ConvertMarkdownToPdf { | |||||||
|         return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); |         return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | class TableAttributeProvider implements AttributeProvider { | ||||||
|  |     @Override | ||||||
|  |     public void setAttributes(Node node, String tagName, Map<String, String> attributes) { | ||||||
|  |         if (node instanceof TableBlock) { | ||||||
|  |             attributes.put("class", "table table-striped"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user