user3245747
user3245747

Reputation: 937

Displaying streamed pdf in iframe

I have a servlet that streams a pdf as such:

ServletOutputStream out = response.getOutputStream();
byte[] pdfByteArray = JasperExportManager.exportReportToPdf(jasperPrintObject);                 
response.reset();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition","inline;filename=temp.pdf");
out.write(pdfByteArray, 0, pdfByteArray.length);
out.flush();
out.close();

This works fine when I call the servlet. Now I use ajax to call the servlet and display the pdf in an iframe. I try to do that as such:

$('#form1').on('submit', function(e) {
     $form = $(this);                                                         
     $.post($form.attr('action'), $form.serialize(), function(responseText) { 
         $('#frm').attr('srcdoc', responseText);
     });
     return false;
});

The iframe however ends up with the following data: %PDF-1.4 %���� 4 0 obj <>stream x���Ko7� �P@�>,َ����I���W{襐m�"K�I�S�(�C{���p� ۝Yv�� �z~��[�á�W��U7-�QT����Uw�M�K�hDpѷ�, Vݓ�8��`�{�����Z��(������((�QX��%A\��E���_����X]�wj����^M��Q'j�nԭ��(����'��+����.��<�Fa�Jx�~T<.¸ ��(����}g��Bx�~ ���!�0ɟR�g�i����,���o'�( ��>:I�������B����pt捎J5.�k�����R㢪B]�u{�Oj��:jT��f)Lj�X\�IKN63���o��T���4g��n�9\��dhIˊ�������s}@�

and it ends with %EOF. In the servlet I have reset the response and set the content type. What else should I do so that the pdf displays properly?

Upvotes: 1

Views: 1548

Answers (1)

user3245747
user3245747

Reputation: 937

I found a solution to the above problem. I kept the servlet code as is but changed the query to the following:

$('#form1').on('submit', function(e) { 
    $form = $(this);
    $.post($form.attr('action'), $form.serialize(), function(responseText) { 
        $('#processing2').fadeOut();
        $('#frm').attr('src', 'theServlet?x=' + $('#x').val() + '&y=' + $('#y').val());                                    
    });
    return false;  
});

So basically instead of using the data in responseText, I re-call the servlet while passing the parameters. Of course, this means that the actual code has to be in the doGet method of the servlet.

Upvotes: 1

Related Questions