Page 1 of 1

Print PDF using Ajax Post response

PostPosted: Mon Mar 03, 2014 7:56 pm
by praveen065
Hi Jason,
The Requirement is to open a PDF file from a jsp without any redirection in single click using Ajax POST.

JSP -> AJAX Post Call -> Server Code returning Response to Ajax -> Print the PDF with the returned data

Ajax Code:
Code: Select all
$.ajax({
            type: "POST",       
            url: "/PrintDocument/PrintServlet?type=pdf",
            data: 'htmlContent='+ escape(someDataToServlet),
            success: function(data){
            alert(data);
                    // To-do block to oepn the PDF
               },
         error:function (data){            
         }   
       });

Servlet Code:
Code: Select all
            FOSettings foSettings = Docx4J.createFOSettings();
            foSettings.setWmlPackage(wordMLPackage);
            Docx4J.toFO(foSettings, response.getOutputStream(), Docx4J.FLAG_NONE);


I'm getting data in the alert or page like below
%PDF-1.4 %ª«¬­ 4 0 obj << /Creator (Apache FOP Version 1.0) /Producer (Apache FOP Version 1.0) /CreationDate (D:20140303114827+05'30') >> endobj 5 0 obj << /N 3 /Length 14 0 R /Filter /FlateDecode >> stream xœ–wTSهϽ7½P’Š”ÐkhRH ½H‘.*1 JÀ"6DTpDQ‘¦2(à€£C‘±"Š…Q±ëDÔqp–Id­ß¼yïÍ›ß÷~kŸ½ÏÝgï}ÖºüƒÂLX € ¡Xáçň‹g` ðlàp³³BøF™|ØŒl™ø½º ùû*Ó?ŒÁÿŸ”¹Y"1P˜ŒçòøÙ\É8=Wœ%

PDF is opening fine if it is URL ajax call.
Please help me in getting the PDF open using ajax-post.
Thanks in advance.

Regards,
Praveen J

Re: Print PDF using Ajax Post response

PostPosted: Mon Mar 03, 2014 10:05 pm
by jason
That looks ok...Just tell the browser what to do with it, via response.setContentType?
Also consider the content-disposition header?

Re: Print PDF using Ajax Post response

PostPosted: Mon Mar 03, 2014 10:21 pm
by praveen065
Hi Jason,
Thanks for your reply. I could see the response holding the PDF data. But could you help in opening that in a window after ajax call.

Code: Select all
$.ajax({
            type: "POST",       
            url: "/PrintDocument/PrintServlet?type=pdf",
            data: 'htmlContent='+ escape(testSt),
            contentType: 'application/pdf',
            success: function(data){

               //What has to be written here

               //alert(data);
               //var myResponse = eval(data);
               //window.open(data);

               },
         error:function (data){            
         }   
       });



Regards,
Praveen_J

Re: Print PDF using Ajax Post response

PostPosted: Tue Mar 04, 2014 8:25 am
by jason
What javascript library are you using?

Google "ajax display pdf"

A common recommendation is to use an iframe:

http://stackoverflow.com/questions/1455 ... -ajax-call

http://stackoverflow.com/questions/1699 ... x-response

http://stackoverflow.com/questions/9840 ... cript?lq=1

Your question isn't really about docx4j, so you might want to post any followup question on StackOverflow instead.