Jasper Reports : JasperReports is an open source Java reporting tool that can write to a various types of report formats such as: PDF, HTML ,Microsoft Excel, RTF, ODT, Comma-separated values or XML files . It can be used in Java-enabled applications, including Java EE or web applications, to generate dynamic content. It reads its instructions from an XML or .jasper file
Ireport : iReport is a GUI tool that helps users and developers that use the JasperReports library to visually design reports.Through a rich and very simple to use GUI, iReport provides all the most important functions to create nice reports in little time. iReport can help people that don't know the JasperReports library create complex reports and learn the XML syntax, taking a look to the generated code. iReport can help skilled report designers compose very complex pages, saving a lot of time. iReport is written in Java.
Requirements :
- Ireport Tool 4.5
- Apache Tomcat Container (up to 7.0)
- Libraries (jar files that needed to create the report from java) Following the list of jar files :
- commons-beanutils-1.8.2.jar
- commons-collections-3.2.1.jar
- commons-digester-1.7.jar
- commons-logging-1.1.jar
- groovy-all-1.7.5.jar
- iText-2.1.7.jar
- jasperreports-4.5.0.jar
- commons-beanutils-1.8.2.jar
- commons-collections-3.2.1.jar
- commons-digester-1.7.jar
- commons-logging-1.1.jar
- groovy-all-1.7.5.jar
- iText-2.1.7.jar
- jasperreports-4.5.0.jar
Steps :
Step 1 : Download and install the ireport tool from http://community.jaspersoft.com/download website .
Step 2 : Open the ireport tool and select the blank A4 template to create the report.
Step 3 : Design the report using ireport tools.
Step 4 : To connect the report with JavaBean Datasource , put the bean jar file in ireport class path Tools -> options -> classpath -> AddJar.
Step 5 : Save the report file at project/WEB-INF/ReportFormat/DemoReport.jrxml and put the project in Apache tomcat webapps directory.
Step 6 : create the jsp file to enter the user detail.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>PDF Generator</title>
</head>
<body>
<h1>Generate PDF file using Jasper Reports </h1>
<form action="addUser" method="post">
<table>
<tr>
<td><label for="firstName">First Name : </label></td>
<td><input type="text" id="firstName" name="firstName"></td>
</tr>
<tr>
<td><label for="lastName">Last Name : </label></td>
<td><input type="text" id="lastName" name="lastName"></td>
</tr>
<tr>
<td><label for="age">Age : </label></td>
<td><input type="text" id="age" name="age"></td>
</tr>
<tr>
<td><label for="sex">Sex : </label></td>
<td>
<select name="sex" id="sex">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="others">Others</option>
</select>
</td>
</tr>
<tr>
<td><label for="address">Address : </label></td>
<td><textarea name="address" id="address" rows="4" cols="20" ></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Report" name="submit"/></td>
<td><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
Step 7 : Create the domain or Java Bean Class to add the detail of user.
package domain;
public class User {
private String firstName;
private String lastName;
private String age;
private String sex;
private String address;
/* public getter/setter */
}
Step 8 : Create the controller to add the list of users in List<User>.
package controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
…………………………….
@WebServlet("/jsp/addUser")
public class AddUser extends HttpServlet{
public void doPost(HttpServletRequest request , HttpServletResponse response)
throws IOException , ServletException{
String submit = request.getParameter("submit");
if(submit.equalsIgnoreCase("submit")){
createUser(request, response);
}else{
processReports(request,response);
}
}
private void createUser(HttpServletRequest request , HttpServletResponse response)
throws IOException , ServletException{
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setAge(age);
user.setSex(sex);
user.setAddress(address);
UserCollections collections = new UserCollections();
collections.addUser(user);
RequestDispatcher view = request.getRequestDispatcher("index.jsp");
view.forward(request, response);
}
.........................................................................................................
}
package tempdatabase;
import java.util.ArrayList;
import java.util.List;
import domain.User;
public class UserCollections {
private static List<User> userList = new ArrayList<User>();
public void addUser(User user) {
userList.add(user);
}
public List<User> getUserList() {
return userList;
}
}
Step 9 : When the List<User> is filled with Users. Generate the pdf report report using following code.
package controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
…………………………….
@WebServlet("/jsp/addUser")
public class AddUser extends HttpServlet{
public void doPost(HttpServletRequest request , HttpServletResponse response)
throws IOException , ServletException{
String submit = request.getParameter("submit");
if(submit.equalsIgnoreCase("submit")){
createUser(request, response);
}else{
processReports(request,response);
}
}
private void processReports(HttpServletRequest request,
HttpServletResponse response) {
File filePath = new File(getServletContext().getRealPath("ReportFormat/DemoReport.jrxml"));
File savePath = new File(getServletContext().getRealPath("WEB-INF/GeneratedReports/userDetail.pdf"));
new ProcessReports().generateReport(filePath , savePath);
String path = savePath.toString();
try{
if(path != null){
response.setContentType("application/pdf");
response.setHeader("content-dispostion","attachment;");
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("WEB-INF/GeneratedReports/userDetail.pdf");
int read = 0;
byte[] bytes = new byte[1024];
OutputStream os = response.getOutputStream();
while((read = is.read(bytes)) != -1){
os.write(bytes,0,read);
}
os.flush();
os.close();
}
}catch(IOException ex){
ex.printStackTrace();
}
}
package tempdatabase;
import java.io.File;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
public class ProcessReports {
public void generateReport(File filePath , File savePath) {
try{
String pdfPath = savePath.toString();
savePath.delete();
JasperDesign design = JRXmlLoader.load(filePath);
JasperReport report = JasperCompileManager.compileReport(design);
JasperPrint print = JasperFillManager.fillReport(report, null,
new JRBeanCollectionDataSource(new UserCollections().getUserList()));
JasperExportManager.exportReportToPdfFile(print, pdfPath);
}catch(JRException ex){
ex.printStackTrace();
}
}
}
Step 10 : After generate the report , the pdf is open in browser.
To download the source code Click .
how to attach this jasper pdf as in mail?
ReplyDelete