0
votes

I want to download a PDF from the website. The PDF needs to be generated within the code, which I thought would be a combination of freemarker and a PDF generation framework like iText. Any better way?

However, main issue is, i dont know how do I allow the user to download a file through a Spring Controller?

1

1 Answers

2
votes
@RequestMapping(value = "/downlaod/{fileName}", method = RequestMethod.GET)
public void getFile(
    @PathVariable("fileName") String fileName, 
    HttpServletResponse response) {
    try {

      InputStream is = ...;  // get uploaded file using InputStream 

      org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());      // copy this to response's OutputStream
      response.flushBuffer();
    } catch (IOException ex) {
      log.info("Error writing file to output stream. Filename was '{}'", fileName, ex);
      throw new RuntimeException("IOError to writing file on output stream");
    }

}

if you having response.getOutputStream(), you can write what you want into there. You have pass this output stream as a place to put generated PDF. you can also set

response.setContentType("application/pdf");