0

I want to display a PDF in the browser. I am using below code to do it.

@Controller
@RequestMapping("/test.pdf")
public class DisplayPDF {

    @RequestMapping(method = {RequestMethod.GET})
    @ResponseBody
    public ResponseEntity<byte[]> start() throws Exception {
        FileInputStream fi = new FileInputStream(new File("/tmp/test.pdf"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int c;
        while ((c = fi.read()) != -1) {
            baos.write(c);
        }

        fi.close();
        byte[] pdf = baos.toByteArray();
        baos.close();

        HttpHeaders headers = new HttpHeaders();

      headers.setContentLength(pdf.length);
      headers.setContentType(MediaType.parseMediaType("application/pdf"));
        headers.set("Content-Disposition", "inline; filename=test.pdf");
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        ResponseEntity<byte[]> responseE = new     ResponseEntity<byte[]>(pdf, headers, HttpStatus.OK);
        return responseE;
    }

I found that, even after setting the header content-type to application/pdf in the above code, my live http headers shows the content as text/html

Output is displayed in the browser as below:

Output in browser

But the below code displays the PDF in the browser.

@RequestMapping(method = {RequestMethod.GET})
public ResponseEntity<byte[]> start() throws Exception {
    FileInputStream fi = new FileInputStream(new File("/tmp/test.pdf"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int c;
    while ((c = fi.read()) != -1) {
        baos.write(c);
    }

    fi.close();
    byte[] pdf = baos.toByteArray();
    baos.close();

    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");

    response.setHeader("Content-Disposition", "inline; filename=sureshbabu.pdf");
    response.setContentType("application/pdf");
    response.setContentLength(pdf.length);
    response.getOutputStream().write(pdf);
    response.getOutputStream().flush();

    return null;
}

What is the difference between these two codes?

jaghan
  • 147
  • 2
  • 12

3 Answers3

0

The same code is working for me. Tested it if different browsers.

Do you have any filters in implementation which might be playing with response headers. Can you also share the log file.

Neeraj Gupta
  • 173
  • 2
  • 14
  • I don't have any filters and special configurations. I tested it on both Firefox and Chrome browsers and it did not work. – jaghan Sep 23 '15 at 07:37
0
  1. Add PDF as a mime type to your server.
  2. Instead of inline, try attachment.
  3. I am not familiar with the API, but wouldn't this work: headers.setContentType("application/pdf");
gn1
  • 526
  • 2
  • 5
0

Maybe it´s too late, but I´m going to give you my idea because I was facing with a similar problem days ago:

In your client side (In my case a JavaScript app I have something like this)

var data = this.toBinaryString(response.responseText);
// Here is the pdf content
data = "data:application/pdf;base64,"+btoa(data);


// Transforming binary data to string
toBinaryString: function(data) {
    var ret = [];
    var len = data.length;
    var byte;
    for (var i = 0; i < len; i++) {
        byte=( data.charCodeAt(i) & 0xFF )>>> 0;
        ret.push(String.fromCharCode(byte) );
    }
    return ret.join('');
},

This workaround works for me and I hope that this code could help you.

Bye!

vcima
  • 421
  • 1
  • 7
  • 20