When generating a PDF, Thymeleaf uses a library called iText or Apache PDFBox to create the PDF document. The generated PDF is then returned as a response to the client.
To set the Content-Disposition header correctly, you can use the following code: taming thymeleaf pdf download
java Copy Code Copied @GetMapping ( ”/download-pdf” ) @ResponseBody public ResponseEntity < byte [ ] > downloadPdf ( Model model ) throws Exception { // … // Set the Content-Disposition header HttpHeaders headers = new HttpHeaders ( ) ; headers . setContentType ( MediaType . APPLICATION_PDF ) ; headers . setContentDisposition ( ContentDisposition . attachment ( ) . filename ( “example.pdf” ) . build ( ) ) ; // Return the PDF document as a response return new ResponseEntity < > ( pdfBytes , headers , HttpStatus . OK ) ; } When generating a PDF, Thymeleaf uses a library
Here’s an example of how you can configure Thymeleaf for PDF download using iText: setContentType ( MediaType