Spring Boot 2: Get html output in controller

G_V :

I'm following the Spring Getting Started tutorials and I'm breaking my brains on how to do something that should be relatively simple like accessing the result of another path in the same Controller.

What I'm trying to do:

  1. Return a filled Thymeleaf template as HTML to the browser <- this works out of the box
  2. Return the same page as a pdf

GreetingController:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.thymeleaf.TemplateEngine;

@Controller
@RequestMapping(path = "/")
public class GreetingController {

    @Autowired private TemplateEngine templateEngine;

    @RequestMapping(value = "/index", method = RequestMethod.GET, produces = "application/html")
    public String html(Model model) {
        model.addAttribute("some_data", some_data.getIt());
        return "some_template";
    }

    @RequestMapping(value = "/pdf", method = RequestMethod.GET, produces = "application/pdf")
    public String pdf() {
        // Option 1: get HTML output from html path
        // Option 2: put the same data in some_template via the template engine and get the resulting HTML
        // write HTML to a file using FileWriter
        // then print the temporary file with HTML to PDF via wkhtml2pdf
        return "generated_pdf";
    }

}`

Maybe I'm going about this all wrong and there is a much easier way to get the filled HTML, please advise.

EDIT:

Gradle dependencies for people trying to do something similar:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}
g00glen00b :

If you're interested in obtaining the generated HTML, the easiest solution is probably to use Thymeleaf's TemplateEngine, like you already did:

Context context = new Context(Locale.getDefault());
context.setVariable("some_data", someData.getIt());
String html = templateEngine.process("some_template", context);

After that, you could process it with any HTML to PDF library. For example, if you're using Flying Saucer, you could write something like this:

try (ServletOutputStream stream = response.getOutputStream()) {
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(stream);
    renderer.finishPDF();
} catch (IOException | DocumentException ex) {
    // Error handling
}

Since ITextRenderer allows you to directly write to an OutputStream, you could use HttpServletResponse.getOutputStream() to do this:

@GetMapping("/pdf")
public void pdf(HttpServletResponse response) {
    // Generate HTML + PDF
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100365&siteId=1