Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i download the PDF file in spring mvc?

this is my file path

public final static String BOOKINGPDFFILE= "D:/Hotels/pdf/";

This below code is what I have written to download pdf from the above resource folder

Pdf="column name in database  i used for storing in database"

@RequestMapping(value = "/getpdf/{pdf}", method = RequestMethod.GET)
public  void getPdf(@PathVariable("pdf") String fileName, HttpServletResponse response,HttpServletRequest request) throws IOException {


   try {
        File file = new File(FileConstant.BOOKINGPDFFILE + fileName+ ".pdf");


        Files.copy(file.toPath(),response.getOutputStream());
    } catch (IOException ex) {
        System.out.println("Contract Not Found");
        System.out.println(ex.getMessage());
    }

}
like image 979
Nikil Karanjit Avatar asked Oct 30 '25 13:10

Nikil Karanjit


2 Answers

Here is the way, hope it help.

@RequestMapping(value = "/getpdf/{pdf}", method = RequestMethod.GET)
public  void getPdf(@PathVariable("pdf") String fileName, HttpServletResponse response) throws IOException {

    try {
        File file = new File(FileConstant.BOOKINGPDFFILE + fileName+ ".pdf");

        if (file.exists()) {
            // here I use Commons IO API to copy this file to the response output stream, I don't know which API you use.
            FileUtils.copyFile(file, response.getOutputStream());

            // here we define the content of this file to tell the browser how to handle it
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf");
            response.flushBuffer();
        } else {
            System.out.println("Contract Not Found");
        }
    } catch (IOException exception) {
        System.out.println("Contract Not Found");
        System.out.println(exception.getMessage());
    }
}
like image 74
Gabriel Villacis Avatar answered Nov 02 '25 03:11

Gabriel Villacis


You may try something like this:

@RequestMapping(method = { RequestMethod.GET }, value = { "/downloadPdf" })
    public ResponseEntity<InputStreamResource> downloadPdf()
    {
        try
        {
            File file = new File(BOOKINGPDFFILE);
            HttpHeaders respHeaders = new HttpHeaders();
            MediaType mediaType = MediaType.parseMediaType("application/pdf");
            respHeaders.setContentType(mediaType);
            respHeaders.setContentLength(file.length());
            respHeaders.setContentDispositionFormData("attachment", file.getName());
            InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
            return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);
        }
        catch (Exception e)
        {
            String message = "Errore nel download del file "+idForm+".csv; "+e.getMessage();
            logger.error(message, e);
            return new ResponseEntity<InputStreamResource>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

And in your web page you can write the link in this way:

<a href="/yourWebAppCtx/yourControllerRoot/downloadPdf" target="_blank"> download PDF </a>
like image 35
Angelo Immediata Avatar answered Nov 02 '25 03:11

Angelo Immediata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!