javascript - Empty and broken pdf file downloaded from UI using spring mvc and apache pdf box -


the following code works fine no error able download file name specified in code issue there no content printed , when open file error saying file damaged. while save file somewhere proper file contents.

from ui:

var jsonstrtosend = json.stringify( jsonobjtosend ); jsonstrtosend = jsonstrtosend.replace(/"/g, "&quot;"); var url = '/'+apppath+'/reportgeneration' ; var $form = $('<form enctype=\'application/json\' action="' + url + '" method="post">').append($('<input type="hidden" name="data" id="data" value="' + jsonstrtosend + '" />')); $form.appendto("body").submit(); 

in controller:

@requestmapping(value = "/reportgeneration", method = requestmethod.post)            public @responsebody void reportgeneration(httpservletrequest request,                  httpservletresponse response){             map returnmapmessage = new hashmap();            int resultdata =0;            httpsession httpsessionobj = request.getsession(false);            try{                pddocument doc = new pddocument();                pdpage intro_page = new pdpage();                 doc.addpage( intro_page );                 pdpagecontentstream contentstream_itro =                         new pdpagecontentstream(doc, intro_page);                 //some stuff.......                 string filename = reportname+"_"+tempdate.getdate()+"-"+tempdate.getmonth()+"-"+tempdate.getyear()+" "+tempdate.gethours()+tempdate.getminutes()+".pdf";                //doc.save("/test/webapp/reports/"+filename);                response.setcontenttype("application/pdf");                pdstream ps=new pdstream(doc);                inputstream is=ps.createinputstream();                string headerkey = "content-disposition";                string headervalue = string.format("attachment; filename=\"%s\"", filename);                response.setheader("expires:", "0"); // eliminates browser caching                response.setheader(headerkey, headervalue);                org.apache.commons.io.ioutils.copy(is, response.getoutputstream());                response.flushbuffer();                is.close();                doc.close(); 

i missing out doc.save() felt not necessary not storing file anywhere in drive. below code works fine.

bytearrayoutputstream output = new bytearrayoutputstream(); doc.save(output); doc.close(); response.addheader("content-type", "application/force-download"); response.addheader("content-disposition", "attachment; filename=\""+filename+"\""); response.getoutputstream().write(output.tobytearray()); 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -