13

I want to return a generated pdf file via spring-mvc-rest controller. This is a shortened version of the code I'm currently using:

@RestController
@RequestMapping("/x")
public class XController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<byte[]> find() throws IOException {
        byte[] pdf = createPdf();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "pdf"));
        headers.setContentDispositionFormData("attachment", "x.pdf");
        headers.setContentLength(pdf.length);
        return new ResponseEntity<byte[]>(pdf, headers, HttpStatus.OK);
    }
}

This works almost fine, it just to return the actual byte array as base64 encoded :(

curl -i 'http://127.0.0.1:8080/app/x'

Server: Apache-Coyote/1.1
Content-Disposition: form-data; name="attachment"; filename=x.pdf"
Content-Type: application/pdf
Content-Length: 138654
Date: Fri, 08 Jan 2016 11:25:38 GMT

"JVBERi0xLjYNJeLjz9MNCjMyNCAwIG9iag [...]

(btw. the response doesn't even contain a closing " :)

Any hints appreciated!

J. Doe
  • 165
  • 1
  • 1
  • 6

5 Answers5

11

The problem is caused by Spring trying to encode the response as Json.

Your request probably specifies Accepts = "*/*" and since Spring ignores the ResponseEntity's ContentType, the best encoding is found to be application/json.

The simplest fix to this is to add a produces to your request mapping, so your code becomes:

@RestController
@RequestMapping(value = "/x",
                produces = "application/pdf") // <-- Add this
public class XController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<byte[]> find() throws IOException {
        byte[] pdf = createPdf();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "x.pdf");
        headers.setContentLength(pdf.length);
        return new ResponseEntity<byte[]>(pdf, headers, HttpStatus.OK);
    }
}
im_infamous
  • 972
  • 1
  • 17
  • 29
Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
  • 1
    Hi, Even i am trying this and working fine. No issue with the code but save as pop up is not appearing on browser. Any help? – Gowthami Reddy Aug 02 '17 at 06:23
  • 1
    It won't open the dialog because application/pdf it's a type that your browser know how to handle. If you change in application/x-download it will work. – ElPysCampeador Jul 07 '20 at 16:13
6

I created the example using your code, but a very similar method is doing his job in my web application:

@RequestMapping(value = "/", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response,
                         HttpServletRequest request) throws IOException
{
    byte[] pdf = createPdf();

    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment; filename=foo.pdf");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.getOutputStream().write(pdf);
}

Else you can try this answer Open ResponseEntity PDF in new browser tab

Community
  • 1
  • 1
ElPysCampeador
  • 399
  • 1
  • 4
  • 14
2

This is my code and work fine, maybe this would can help you.

@RequestMapping(value = "/createReport", method = RequestMethod.POST,produces="application/pdf")
    @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<byte[]> createReport(@RequestBody ReporteDTO reporteDTO) {
        byte[] outputReport = null;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/pdf"));
        headers.setContentDispositionFormData("inline", "archivo.pdf");
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
             outputReport = getFilePdf();
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(outputReport, headers, HttpStatus.OK);
        return response;
    } 
jose rivera
  • 463
  • 2
  • 6
  • 14
1

Add produces property to RequestMapping:

@RequestMapping(path = "/download", produces = "application/pdf")
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

The answer from Rasmus Faber is very close.

In my case: I want to view a PDF generated by spring. I don't want to download it.
But I get every time the base64 encoded string, instead of my byte[].

I found out that I get the base64-string only in Firefox. So I switched to chrome, where the answer from Rasmus working fine.
Now, it works in Firefox too. But I can't say why (I changed nothing). I guess that something was cached.

For more information, this might help Incorrect content type for PDF file with Firefox

My working code with streams:

@GetMapping(path = "/demo/pdf", produces = "application/pdf")
public StreamingResponseBody getPdf(final HttpServletResponse response)  {
    response.setContentType("application/pdf");
    response.setHeader("cache-control","must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Content-Disposition", "inline; filename=aCleverFileName.pdf");
    return outputStream -> this.pdfService.createPdf(outputStream);
}
akop
  • 5,981
  • 6
  • 24
  • 51