Tuesday, December 20, 2011

Jasper report printing at client side,Java,Struts,jasper reports, print at client side,Ajax


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(); %>








Tuesday, January 25, 2011

Calling Procedures from Java

Google Map with multiple Markers used to locate different locations .
We can show a map with image and contact details like following