java - Download a generated pdf from client -


i'm new in stackoverflow first post. please excuse poor english... have spent hours on related topics no anwser met needs. i'm working on address book , want users able download pdf contacts list whenever want. seems go in fact client never download pdf file, though can see in request using chrome developer tool. here code:

public void dopost( httpservletrequest request, httpservletresponse response ) throws ioexception, servletexception {      try {         document document = new document();         pdfwriter.getinstance( document, response.getoutputstream() );         document.open();         document.add( new paragraph( "you've selected " + request.getparameter("number") + " contacts." ) );          response.setcontenttype("application/pdf");         response.setheader( "content-disposition", "attachment; filename=\"contacts.pdf\"" );          document.close();     }     catch( documentexception e ) {         e.printstacktrace();     } } 

when examining response, this:

 %pdf-1.4 %���� 2 0 obj >stream x�+�r �26s�00s 5 0 obj > endobj 6 0 obj > endobj xref 0 7 0000000000 65535 f  0000000320 00000 n  0000000015 00000 n  0000000408 00000 n  0000000163 00000 n  0000000459 00000 n  0000000504 00000 n  trailer ]/info 6 0 r/size 7>> %itext-5.4.1 startxref 645 %%eof 

does have kindness tell me what's wrong ?

you should call document.close(), before setting response headers, here detailed example writing explicitly outputstream of servlet

hope helps.

-- [edit] ran same code given in question itext-2.1.7 doget, works me, issue using servlet method uses post http request(dopost), while if hitting basic url in server directly, send request.

you can either override service method, or call dopost doget, or submit same url form, or ajax, ensure http post call.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -