13

i am trying to display a pdf in my browser with spring. my problem is that the browser downloads the file instead of displaying it. this is my code:

@RequestMapping(value="/getpdf1", method=RequestMethod.GET)
public ResponseEntity<byte[]> getPDF1() {


    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "pdf1.pdf";

    headers.add("content-disposition", "inline;filename=" + filename);

    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(pdf1Bytes, headers, HttpStatus.OK);
    return response;
}

i am looking forward to your answers! thanks

cmplx96
  • 1,541
  • 8
  • 34
  • 48

6 Answers6

25

Content-Disposition to control either download or view in browser

View in browser

headers.add("Content-Disposition", "inline; filename=" + "example.pdf");

Download

headers.add("Content-Disposition", "attachment; filename=" + "example.pdf");
WLiu
  • 426
  • 4
  • 10
  • I'm using this same code for image/png, but this is still downloading. – Ankit Oct 13 '22 at 06:49
  • but this solution was talking about PDF file, can you provide more information for your code about image/png file? – WLiu Oct 21 '22 at 01:58
14

You are almost done. Just remove

headers.setContentDispositionFormData(filename, filename);

from your code block. It should be used when posting multipart/form-data to the server.

ksokol
  • 8,035
  • 3
  • 43
  • 56
  • I tried like this but still its downloading instead of view in browser. what was the exact solution here. – MMMMS Dec 20 '16 at 07:17
  • 1
    Solution: Code block from OP's question but without `setContentDispositionFormData`. Maybe you should post a new question. – ksokol Dec 20 '16 at 12:06
  • Off topic, but do you happen to know how to remove the spring leaf icon (web browser icon) from this pdf viewer when it 's opened in a new tab? – JayC Jan 28 '20 at 17:42
  • Put your favicon at the root of your web site, e.g. http://localhost/favicon.ico. See also https://www.w3.org/2005/10/howto-favicon. – ksokol Jan 28 '20 at 19:51
2

Below code Snippet will assist you display the file on the browser


@GetMapping(value = "/terms-conditions")
    public ResponseEntity<InputStreamResource> getTermsConditions() {

        String filePath = "/path/to/file/";
        String fileName = "fileName.pdf";
        File file = new File(filePath+fileName);
        HttpHeaders headers = new HttpHeaders();      
        headers.add("content-disposition", "inline;filename=" +fileName);
        
        InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/pdf"))
                .body(resource);
    }
Amimo Benja
  • 505
  • 5
  • 8
0

it's working you should try do that, get by filename and opnend in browers

  @GetMapping(value = "/pdf/{filename:.+}") public ResponseEntity<InputStreamResource> getTermsConditions(@PathVariable String filename) {

    File file = storageService.load(filename).getFile();
 
    HttpHeaders headers = new HttpHeaders();      
    headers.add("content-disposition", "inline;filename=" +fileName);
    
    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

    return ResponseEntity.ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.parseMediaType("application/pdf"))
            .body(resource);
}
-1

See code bellow:

headers.setContentDisposition(ContentDisposition.inline().filename("example.pdf").build());
Clairton Luz
  • 2,116
  • 19
  • 15
-1

View in browser (use inline) :

header.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + pdfFile.getName());

For Download (use attachment) :

header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + pdfFile.getName());
Elikill58
  • 4,050
  • 24
  • 23
  • 45