Title: How to render pdf generated from jasper reports in html <object> tag
Tags: struts2;jasper-reports;pdf-generation
Question: I am working on a struts2 project and come across a problem to display pdf report generated by JasperReport Server in the <object&gt; tag.
Following are the jars i am using:
struts2-core-2.3.16.jar
jasperreports-5.5.0.jar
jasper-compiler-jdt-5.5.15.jar
and many other jars.
Following is the code snippet for my action method.
public String salesReportHtml() {
try {
...
for (int i = 0; i < tables.getTable("RS1").size(); i++) {
for (int j = 0; j < tables.getTable("RS2").get(i).size(); j++) {
System.out.println(j);
head.put(headerColumn.get(j), tables.getTable("RS1").get(i).get(headerColumn.get(j)));
}
}
for (int i = 0; i < tables.getTable("RS2").size(); i++) {
field = new HashMap<&gt;();
for (int j = 0; j < tables.getTable("RS2").get(i).size(); j++) {
field.put(headerColumn.get(j), tables.getTable("RS2").get(i).get(headerColumn.get(j)));
}
datasourceList.add(field);
}
System.out.println("size datasourceList=&gt;" + datasourceList);
baos =new ByteArrayOutputStream();
JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(datasourceList);
JasperPrint print = JasperFillManager.fillReport(
request.getServletContext().getRealPath("") + "/SCT/resources/newexcel.jasper", head, dataSource);
JasperExportManager.exportReportToPdfStream(print, baos);
reportStream=new ByteArrayInputStream(baos.toByteArray());
System.out.println("done");
return "success";
} catch (HibernateException e) {
e.printStackTrace();
return "error";
} catch (SQLException e) {
e.printStackTrace();
return "error";
catch (Exception e) {
printStatement.print("in exception");
e.printStackTrace();
return "error";
Here is the struts config for the action.
<action name="salesReportPdf" class="com.mindcraft.sct.action.SalesReportAction"
method="salesReportPdf"&gt;
<result name="success" type="stream"&gt;
<param name="contentType"&gt;application/pdf</param&gt;
<param name="inputName"&gt;reportStream</param&gt;
<param name="contentDisposition"&gt;attachment;filename="sales.pdf"               </param&gt;
<param name="bufferSize"&gt;1024</param&gt;
</result&gt;
<result name="error"&gt;/SCT/jsp/ErrorJasper.jsp</result&gt;
</action&gt;
And i have made an ajax call for the action and proccess the data recieved.
<html&gt;
<body&gt;
...
<div id=pdfDiv&gt;
<object id=pdfview width="600" data="" type="application/pdf"&gt;</object&gt;
</div&gt;
</body&gt;
</html&gt;
function exportType() {
debugger;
var type = $('#reportid').val();
alert(type);
if (type == "pdf") {
alert("in pdf condition");
$.ajax({
url : "salesReportPdf.action",
async : false,
}).done(function(data) {
alert("success");
var pdf = data;
alert(pdf);
$('#pdfview').html('<object data="'+ pdf + '"/&gt;');
//$('#pdfview').attr('data', pdf);
});
}
but i am getting a gibberish data in the  tag.
��#����??������������F����x��w1��?����ߠ=���?����#�^��5O���N����N�|���d��f��"�����i��i��II?r�?�:��m6n���U�?���<������VhYJ#�=��qx�bj���3��>��ھ����cmOu|�q��c+�>��J����Z������ͯQo�����P��F��/�5)�U#���W��b@0}M�e+_s���A�R�� ��Yj;f�(�k ���^vZ/(�3���/�CJ+�gN��-�ҭ����ջMl)?4q�\$�ņV˱P,��k�C�Ɛ�����\1�Y��٢��n�-��aY�XZ ���Ǻt�X!����``�:��0MNWs�n��Xj�g�d��y�+1�%�!5��|m!c��Njԓe��7� z�4�b�� �<ԋ�Ń4�@��%��c��n,�q:��&gt;_���D�?V�ng�z[_��@�i�b�-c`��>]_O�w�o����|NA�2�����1��W66v��C��4Jڍ��\���&amp;�o4 �C*��� �*aD?N���{ă֡�6�H�1-�.eC������:']B{#�Ԋj�g{0��� �E�������h��VI�>�O�F��z�#�BEb$H1� _b4�p�1P�����I;�摢>��(E
Please help me to find a solution for this.
Comment: @RomanC but i am using export to pdfStream and not html. And can u please explain it in more detail.
Thanks.
And to mention the action method is salesReportPdf and not salesReportHtml i wrote that by mistake

Comment: This is wrong format. You should use a dispatcher to render html.

Comment: use ` inline;filename="test.pdf"`



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:06/10/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<struts2><jasper-reports><pdf-generation>', 'Title': 'How to render pdf generated from jasper reports in html <object> tag', 'ViewCount': 409, 'id': 37746729, 'user_AboutMe': None, 'user_AccountId': 8610787.0, 'user_DisplayName': 'aniket', 'user_Reputation': 11.0, 'user_Views': 11.0, 'user_id': 6449311.0}}