BAEL-3826: creating PDFs using Thymeleaf as template engine
This commit is contained in:
		
							parent
							
								
									7b071a05e4
								
							
						
					
					
						commit
						05a4f60e38
					
				
							
								
								
									
										10
									
								
								pdf/pom.xml
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								pdf/pom.xml
									
									
									
									
									
								
							| @ -60,6 +60,16 @@ | ||||
|             <artifactId>poi-ooxml</artifactId> | ||||
|             <version>${poi-ooxml.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.thymeleaf</groupId> | ||||
|             <artifactId>thymeleaf</artifactId> | ||||
|             <version>3.0.11.RELEASE</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.xhtmlrenderer</groupId> | ||||
|             <artifactId>flying-saucer-pdf</artifactId> | ||||
|             <version>9.1.20</version> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <build> | ||||
|  | ||||
							
								
								
									
										48
									
								
								pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,48 @@ | ||||
| package com.baeldung.pdf; | ||||
| 
 | ||||
| import com.lowagie.text.DocumentException; | ||||
| import org.thymeleaf.TemplateEngine; | ||||
| import org.thymeleaf.context.Context; | ||||
| import org.thymeleaf.templatemode.TemplateMode; | ||||
| import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; | ||||
| import org.xhtmlrenderer.pdf.ITextRenderer; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.FileOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.OutputStream; | ||||
| 
 | ||||
| public class PDFThymeleafExample { | ||||
| 
 | ||||
|     public static void main(String[] args) throws IOException, DocumentException { | ||||
|         PDFThymeleafExample thymeleaf2Pdf = new PDFThymeleafExample(); | ||||
|         String html = thymeleaf2Pdf.parseThymeleafTemplate(); | ||||
|         thymeleaf2Pdf.generatePdfFromHtml(html); | ||||
|     } | ||||
| 
 | ||||
|     public void generatePdfFromHtml(String html) throws IOException, DocumentException { | ||||
|         String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf"; | ||||
|         OutputStream outputStream = new FileOutputStream(outputFolder); | ||||
| 
 | ||||
|         ITextRenderer renderer = new ITextRenderer(); | ||||
|         renderer.setDocumentFromString(html); | ||||
|         renderer.layout(); | ||||
|         renderer.createPDF(outputStream); | ||||
| 
 | ||||
|         outputStream.close(); | ||||
|     } | ||||
| 
 | ||||
|     private String parseThymeleafTemplate() { | ||||
|         ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); | ||||
|         templateResolver.setSuffix(".html"); | ||||
|         templateResolver.setTemplateMode(TemplateMode.HTML); | ||||
| 
 | ||||
|         TemplateEngine templateEngine = new TemplateEngine(); | ||||
|         templateEngine.setTemplateResolver(templateResolver); | ||||
| 
 | ||||
|         Context context = new Context(); | ||||
|         context.setVariable("to", "Baeldung.com"); | ||||
| 
 | ||||
|         return templateEngine.process("thymeleaf_template", context); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										7
									
								
								pdf/src/main/resources/thymeleaf_template.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								pdf/src/main/resources/thymeleaf_template.html
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> | ||||
|   <body> | ||||
|     <h3 style="text-align: center; color: green"> | ||||
|       <span th:text="'Welcome to ' + ${to} + '!'"></span> | ||||
|     </h3> | ||||
|   </body> | ||||
| </html> | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user