Generate a PDF page using Java on the back and angular on the front

Asked

Viewed 592 times

-1

I would like the help of someone, I am developing a system using Java with Spring Boot framework, and Angular on the front, and I would like to generate a PDF with a predefined template, just filling it with the information populated in a simple CRUD. I’d like to know some method that can help me with that.

1 answer

0


You can do this with Itext and Thymeleaf.

In your controller you will receive one HttpServletResponse responsible for the response stream of your PDF. In the class that will do the processing you must inject a SpringTemplateEngine, which in the example was attributed to the templateEngine.

Variables will be set in your html template in the same way as with Thymeleaf.

The variable nomeTemplate is the name of the html file in your /Resources folder. For example: report.html. It must be a valid Thymeleaf template!

    public void download(String nomeTemplate, HttpServletResponse response){
        Context context = new Context();

        Map<String, Object> variaveis = getAlgumasVariaveis();

        variaveis.forEach(context::setVariable);

        String html = templateEngine.process(nomeTemplate, context);

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(html);
        renderer.layout();
        try {
          renderer.createPDF(outputStream);
        } catch (DocumentException e) {
         e.printStackTrace();
       }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.