1

I am using ajax to send request to my servlet to generate a report.

The report is getting generated but I want to display it in browser

This is my ajax code for request

$.post('servleturl',{
            from : from,
            to : to
        },function(data){
            // What should I write here in order to display report in browser

        });

My servlet Code

OutputStream outputstream = response.getOutputStream();

//This line converts the report to PDF
report.toPdf(outputstream);
outputstream.close();

//Converting the outputstream to JSON to pass as response

                    json = new Gson().toJson(outputstream);            
                    response.setContentType("application/json");
                    response.getWriter().write(json);
timothyclifford
  • 6,799
  • 7
  • 57
  • 85
Nishant123
  • 1,968
  • 2
  • 26
  • 44
  • Your issue will be that you're returning the PDF serialized as JSON rather than the actual file bytes. Do you want to render the browser in the current view? If so, you could look at one of the JavaScript libraries such as http://mozilla.github.io/pdf.js/ otherwise you could generate the PDF, save it somewhere on the server and return the path to your JavaScript which would render it as a link on the page. Few options, depends how you want it to work exactly. – timothyclifford Jan 29 '15 at 12:30
  • Don't encapsulate a binary file like PDF in JSON. Just send it as binary stream to the client. – Robert Jan 29 '15 at 12:31
  • see http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post – ptrk Jan 29 '15 at 12:34
  • Can anyone give an example or sample code to open a file in browser through ajax? – Nishant123 Jan 30 '15 at 08:39

0 Answers0