Process HTML file using Thymeleaf in Web based Scopes of Spring and store the processed template as String
See update below
When you configure Thymeleaf you should define the template engine and the template resolver otherwise when you autowire defaults are used. If you create an instance every time it is not a good practice. Here a sample configuration:
@Configuration
@EnableWebMvc
public class ThymeleafConfiguration {
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
SpringResourceTemplateResolver templateResolver
= new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
}
Then if you want to experiment programmatically you can autowire them but the usual flow for serving html pages with thymeleaf is to define the view resolver as well:
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
With all that in place you can write a controller as:
@Controller
public class MyController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "yourTemplateName";
}
}
Parameters can be passed to the template by using model attributes.
UPDATE 31/07/2018
unfortunately I do not have the time to complete working proof of concept, however I think the below code is enough to show the flow. If you run it and call localhost:8080/test
you should be able to see the output html in the console. The pdf generation can be added as a view resolver and/or invoked programmatically, in this example using xhtmlrenderer
; I do not have the time to complete it so I commented it out but you can get the point: a service provide the html and pdf generation by autowiring the template engine.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.paizo</groupId>
<artifactId>html2pdf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>html2pdf</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ThymeleafConfiguration.java
@Configuration
public class ThymeleafConfiguration {
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public ClassLoaderTemplateResolver thymeleafTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
}
HelloWorldController.java
@Controller
public class HelloWorldController {
@Autowired
private Html2PdfService pdfService;
@GetMapping(path = "/test")
public String hello() {
Map parameters = new HashMap();
parameters.put("name", "Borat");
System.out.println(pdfService.template2Html("test", parameters));
return "test";
}
// @ResponseBody
// @GetMapping
// public ResponseEntity helloPdf() {
// Map parameters = new HashMap();
// parameters.put("name", "Borat");
// pdfService.template2Pdf("test", parameters);
// String filePath = "PATH_HERE";
// InputStream inputStream = new FileInputStream(new File(filePath));
// InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
// HttpHeaders headers = new HttpHeaders();
// headers.setContentLength();
// return new ResponseEntity(inputStreamResource, headers, HttpStatus.OK);
// }
}
Html2PdfService.java
@Service
public class Html2PdfService {
@Autowired
private TemplateEngine templateEngine;
public OutputStream template2Pdf(String templateName, Map parameters) {
// OutputStream outputStream = new BufferedOutputStream();
// IOUtils.copy()
//
// Context ctx = new Context();
// String processedHtml = templateEngine.process(templateName, ctx);
// ITextRenderer renderer = new ITextRenderer();
// renderer.setDocumentFromString(processedHtml);
// renderer.layout();
// renderer.createPDF(os, false);
// renderer.finishPDF();
return null;
}
public String template2Html(String templateName, Map parameters) {
Context ctx = new Context();
ctx.setVariable("name", "pippo");
String processedHtml = templateEngine.process(templateName, ctx);
return processedHtml;
}
}
Html2pdfApplication.java
@SpringBootApplication
public class Html2pdfApplication {
public static void main(String[] args) {
SpringApplication.run(Html2pdfApplication.class, args);
}
}
as a side note if you plan to generate the pdf on the fly and serve it as response in the controller I suggest to use streams and not byte arrays or temp files.
If TemplateEngine
is autowired inside a singleton
bean, then the below mentioned code works perfect.
@Controller
public class jataController {
@Autowired
private TemplateEngine templateEngine;
@GetMapping(value = "/manual-thym")
@ResponseBody
public void justSample() {
Context context = new Context();
String filename = "templates/view/generated-ticket.html";
String html = renderHtml(filename, context);
System.out.println("template\n" + html);
}
private String renderHtml(String filename, Context context) {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
templateResolver.setOrder(1);
templateResolver.setCharacterEncoding("UTF-8");
templateEngine.setTemplateResolver(templateResolver);
String html = templateEngine.process(filename, context);
return html;
}
}
but if TemplateEngine
is autowired on a request
scope bean type, it gives exception and thymeleaf
will create a memory leak. So finally with lots of hit and tries, i got a working solution and thanks to @Paizo. It might contain some bad practice but this is how it worked:
@Controller
@Configuration
@EnableWebMvc
@ApplicationScope
public class MyThymeleafConfig {
@GetMapping("/view-template")
@ResponseBody
public void viewTemplates() {
Context context = new Context();
context.setVariable("mydata", "this is it");
String html = templateEngine().process("templates/view-to-process.html", context);
System.out.println(html);
}
/*
configuration for thymeleaf and template processing
*/
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:");
templateResolver.setSuffix(".html");
templateResolver.setCacheable(false);
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
}
and to serve the static resources we need to define another bean, which is as follows:
@Configuration
@EnableWebMvc
public class StaticResourceConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("/static/", "classpath:static/");
}
}
This solution cannot be found on any forum. However i would ask as well as request any Spring
developer to provide a better implementation for the above code.
I'm using similar Springboot and Thymeleaf versions and something like this worked for me on a few projects :
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@Component
public class EmailProcessor {
private TemplateEngine htmlTemplateEngine;
@Autowired
public EmailProcessor(TemplateEngine templateEngine) {
this.htmlTemplateEngine = templateEngine;
}
public String process(User user) {
final Context ctx = new Context();
if (user != null) {
ctx.setVariable("user", user);
}
return htmlTemplateEngine.process("emails/template", ctx);
}
}
Email template is just a regular Thymeleaf template which is located in:
resouces/templates/emails/template.html