I am trying to render an HTML file using thymeleaf and keep the resultant HTML content in a String variable in web-based scopes of Spring so that i can use it l
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
4.0.0
com.github.paizo
html2pdf
0.0.1-SNAPSHOT
jar
html2pdf
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.xhtmlrenderer
flying-saucer-pdf
9.1.14
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
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.