Thursday, February 4, 2010

Way of printing Jasper Report at client side

Hi friends,

I have developed one project which is customised Jasper Reports with export and print options.
I haven't get any problem for export options.
Coming to Print, i found that it is printing at server end.
So to eliminate that problem i found one solution that will be printing at client side.

For this solution i have used Jasper Report object and Ajax functionality.
The code is like this:
1) In my JSP



<input type="button" class="button_submit" value="print" onclick="javascript:print();">


Handler: For a "Print" Button you need to add onclick listener function print();


function print()
{
/* ========== AJAX Initializtion ========== */
xmlhttp = false;
if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
/* ========== AJAX Initializtion ========== */

if (xmlhttp!=null) {
url = "/mms/reports";
xmlhttp.onreadystatechange= function () { getWorkReqDivs(xmlhttp); };
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Cache-Control","no-cache, private, max-age=0");
xmlhttp.send(null);

} else {
alert("Your browser does not support XMLHTTP.");
}
}
function getWorkReqDivs(httpRequest1)
{
if(httpRequest1.readyState==4)
{
if(httpRequest1.status==200)
{
var result = httpRequest1.responseText;
var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";
var content_vlue=result;
var docprint= window.open("", "", disp_setting);
docprint.document.open();
docprint.document.write(&lthtml>);
docprint.document.write(<body onLoad="self.print()">&lt

/body>
&lt
/html>);

docprint.document.write(content_vlue);

docprint.document.close();
docprint.focus();

}
}
}

This script is used to get the Jasper Report in HTML format as responseText. So this text is added in window.open().It looks like print preview.In onload itself print dialog box will come to ask print.


Struts-config.xml


<action path="/mms/reports" type="com.km.bp.mms.jsperReports.printAction"> 
<forward name="print" path="/WEB-INF/jsp/jasperReports/print.jsp"/>
</action>


Java Class: (printAction.java)


JRHtmlExporter exporter = new JRHtmlExporter();

String jrept = "indentReport.jrxml";

String reportFileName = JasperCompileManager.compileReportToFile(request.getRealPath("/reports") + "/" + jrept);

java.util.Map parameters = new java.util.HashMap();

Connection con = MMSDB.getConnection();String fromDatet="01/01/2010";String toDate="28/02/2010";

parameters.put("fromDate", fromDatet);parameters.put("toDate", toDate);

File reportFile = new File(reportFileName);if (!reportFile.exists()) {throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
}

JasperPrint jasperPrint = JasperFillManager.fillReport(reportFileName, parameters, con);

PrintWriter out = response.getWriter();response.setContentType("text/html");

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, out);

HashMap fontMap = new HashMap();

exporter.setParameter(JRHtmlExporterParameter.FONT_MAP, fontMap);

exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);

exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);

exporter.setParameter(JRHtmlExporterParameter.IS_WRAP_BREAK_WORD, Boolean.TRUE);

request.setAttribute("exportIndentObject", exporter);

return mapping.findForward("print");}

}


  



Jsp File :(print.jsp)( The following Jsp is the responseText of the Ajax Call)


<%@page import="net.sf.jasperreports.engine.export.JRHtmlExporter" %>

< % JRHtmlExporter export = (JRHtmlExporter) request.getAttribute("exportIndentObject"); session.setAttribute("exportIndentObject", export); JRHtmlExporter export1 = (JRHtmlExporter) session.getAttribute("exportIndentObject"); export.exportReport(); %>








19 comments:

  1. Hi there,

    I'm facing today the same problem as yours.

    But instead of struts, i'm working with jsf.

    Is there a solution for my problem ?

    ReplyDelete
  2. Hi Midos,

    I tried to implement. But it will take some more time. Because i am not familiar with JSF.
    After successful completion i will post ur problem in my blog.

    ReplyDelete
  3. Hi. What do I need for this sentence? My Eclypse doesn't found definition.

    Connection con = MMSDB.getConnection();

    ReplyDelete
  4. No.No.
    "MMSDB" is a public class.In that i have written steps to connect with database.
    Instead of that(MMSDB) you can use like this
    Connection conn = DriverManager.getConnection(url,"user1","password");

    ReplyDelete
  5. Hi Miral,

    Please check the answer available in the next post

    url is

    http://helptodeveloper.blogspot.com/2010/02/exporting-jasper-reports-with-save-as.html

    ReplyDelete
  6. Hi,

    The css is not displayed when print

    ReplyDelete
  7. Hello
    i want to print some specific data of my web page,, created through struts 2,,, i need to print that data by clicking the Button(Print), on web page. Im not getting the idea to do it thoroughly,,, kindly reply me at dilgul_morio@hotmail.com.
    Thanx in advance

    ReplyDelete
  8. Hi,

    Do you want to send request to server by clicking Print button? (Means handling printer through server side) or client side (through javascript)?

    If u want to print web page data through javascript , code is in the following URL.Please check javascript code

    http://helptodeveloper.blogspot.com/2010/02/way-of-printing-jasper-report-at-client.html


    Let me know otherwise.

    ReplyDelete
  9. Can I use this functionality in Oracle Apex?

    ReplyDelete
  10. Hello, I also have a problem printing jasper reports on the client side.
    I use struts2. you have not yet found a solution? please reply to nasmus@mail.ru

    ReplyDelete
  11. Hi,

    This is the marvellous post that I have come over after huge searches. I am really thankful to you for providing this unique information. Thanks a lot.

    Web Harvesting

    ReplyDelete
  12. can you provide the source code for download...

    ReplyDelete
  13. could you please tell me how the url path define ? mms/report

    ReplyDelete
  14. Thank you very much for sharing the process and code.

    ReplyDelete
  15. Hi...

    Please Send me full code of this example...
    riyadave1@yahoo.com

    Thanks...
    Riya..

    ReplyDelete
  16. How can I do this with Servlets??

    Thanks
    Masood

    ReplyDelete
  17. Its like you read my mind! You seem to know so much about this, like you
    wrote the book in it or something. I think that you can do with a few pics to drive
    the message home a little bit, but other than that,
    this is wonderful blog. A great read. I will definitely
    be back.

    Look into my web page ... Knights N Squires Hack android download

    ReplyDelete