1

Trying to send a PDF from a servlet to the client. The client sends a POST request through AJAX, and the request is handled by the servlet to generate a PDF and sends the PDF as a response to the client.

I've tried the options posted here to no avail (getting empty/un-openable pdfs): Opening PDF String in new window with javascript

Any help would be apprecieated!

So far, I can only get a formatted PDF String printing in the browser console using this code:

Java Servlet:

    response.setContentType("application/pdf");
    response.setHeader("Content-disposition","attachment; filename=ProgressReport.pdf");
    response.setContentLength((int) pdfFile.length());

    OutputStream out = response.getOutputStream();
    FileInputStream in = new FileInputStream(pdfFile);
    byte[] buffer = new byte[1024];
    int length =0;
    while ((length = in.read(buffer)) != -1){
       out.write(buffer, 0, length);
       System.out.println(buffer);
    }
    in.close();
    out.flush();

JS

                $.ajax({
                url : "GenerateReport",
                data : {
                         ...
                },
                type : "POST",
                success : function(result) {
                    console.log(result);
                    //window.open("data:application/pdf, " + encodeURI(result)); 
                    //download(result);
                },
                error : function(result) {
                    ...
                }
            })

PDF String in Browser Console

%PDF-1.4 %����3 0 obj<</Filter/FlateDecode/Length 238>>streamx��QMO�@��x��(��D��!A�x�R��T�-�n��{�5LDB�e2�y�>2�l�Y$1�:a�i.�"�~f ...
Community
  • 1
  • 1
Keval Vyas
  • 33
  • 1
  • 5

1 Answers1

0

I'm not 100% but your window.open is not the best as pop up blockers might prevent it as it's not a user action calling it's an AJAX response.

The better way would be the method outlined in this answer

var hiddenElement = document.createElement('a');

hiddenElement.href = 'data:attachment/text,' + encodeURI(result);
hiddenElement.target = '_blank';
hiddenElement.download = 'myFile.txt';
hiddenElement.click();

The other option is to use Base64 encoding and use "data:image/png;base64,"+result and in your C# you would need to create the buffer the size of the file and then base64 encode the entire buffer

Community
  • 1
  • 1
Barkermn01
  • 6,781
  • 33
  • 83
  • Hi, I've edited my servlet code to encode the file as Base64, and using ` "data:application/pdf;base64"+result;` However, this is just downloading a blank PDF. Do you know why this could be? – Keval Vyas Mar 06 '17 at 13:10
  • If I decode result first using `window.atob(result)` and pass it using `"data:application/pdf;"+result;`, it opens a blank PDF too – Keval Vyas Mar 06 '17 at 13:21
  • I'm sorry then i'm unsure what happens if you decode the `base64` on this host and save the file is it the correct size? – Barkermn01 Mar 06 '17 at 14:52
  • The last ling a can suggest is instead of using `FileInputStream` use `BinaryReader` and `BinaryWriter` you should be able to convert your streams to them – Barkermn01 Mar 10 '17 at 10:43