0

I have a web service that generate a pdf. In my GAE application I have a button, when i click i use an ajax's function.

$('#test').click(function(){
        $.ajax({
            url: 'provaws.do',
            type: 'get',
            dataType: 'html',
                    success : function(data) {
                    }
        });
    });

this is the method in java that's call ws, using UrlFetch:

    @RequestMapping(method = RequestMethod.GET, value = PROVAWS_URL)
public void prova(HttpServletRequest httpRequest, HttpServletResponse httpResponse, HttpSession httpSession) throws IOException{
    try {
        URL url = new URL("http://XXXXX/sap/bc/zcl_getpdf/vbeln/yyyyyy");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization","Basic " + Base64.encodeBase64String(("username:password").getBytes()));
        connection.setConnectTimeout(60000);
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            // OK
            ByteArrayOutputStream bais = new ByteArrayOutputStream();
            InputStream is = null;
            try {
              is = connection.getInputStream();
              byte[] byteChunk = new byte[4096];
              int n;

              while ( (n = is.read(byteChunk)) > 0 ) {
                bais.write(byteChunk, 0, n);
              }
            }
            catch (IOException e) {

            }
            finally {
              if (is != null) { is.close(); }
            }
            httpResponse.setContentType("application/pdf");
            httpResponse.setHeader("content-disposition","attachment; filename=yyyyy.pdf");             
            httpResponse.getOutputStream().write(bais.toString().getBytes("UTF-8"));
            httpResponse.getOutputStream().flush();         
        }
....
}

With Firebug i see the repsonse:

%PDF-1.3
%âãÏÓ
2 0 obj
<<
/Type /FontDescriptor
/Ascent 720
/CapHeight 660
/Descent -270
/Flags 32
/FontBBox [-177 -269 1123 866]
/FontName /Helvetica-Bold
/ItalicAngle 0
....

What i need to set in ajax's function to show the pdf?

Thanks in advance

Giacomo Savioli
  • 167
  • 3
  • 6
  • 14
  • does it need to be an ajax request - why not just a normal http request ? you're setting content-disposition to attachment so you won't lose the loaded DOM. – planetjones Mar 22 '13 at 14:01
  • my error was httpResponse.getOutputStream().write(bais.toString().getBytes("UTF-8"));.. And i'll go out from ajax.. tks – Giacomo Savioli Mar 22 '13 at 16:49

1 Answers1

0

I don't know Java well, but in my understanding your mechanism may not be right.

Here are my corrections:

Instead of sending files in stream, the server-side code(JAVA) should generate the pdf at backend, put the file in file system, and then return the URI of file to Ajax response.

For Ajax code, it get the url from server, then show the new url in DOM. Then user can follow this link to read/download PDF.

Side note:

I checked further that there are methods for streaming data by Ajax, though jQuery's ajax() can't handle that. But I think for a PDF file rendering, streaming is overkill. Refs: jquery ajax, read the stream incrementally?, http://ajaxpatterns.org/HTTP_Streaming#In_A_Blink*

Community
  • 1
  • 1
Billy Chan
  • 24,625
  • 4
  • 52
  • 68