Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Overview
2. Requirements
3. POC Activities
4. Advantages
5. Disadvantages
6. Tools
Overview
Jasper Reports is open source reporting tool.
• Jasper Reports is written in 100% Java. And can be embedded in any Java
application.
• JasperReports is an engine that takes an XML file and forms a report out of that
file using the data source specified in the XML file.
• Various output options PDF, HTML, XLS, CSV, XML, RTF, TXT files, or
directly on the screen or printer.
• Various data source options: JDBC, EJB, POJO, Hibernate, XML.
• Integrated charting
Features
Requirements
•Jar Files
1. ojasperreports-2.0.2
2. ocommons-beanutils-1.5
3. ocommons-collections-2.1
4. ocommons-digester-1.3
5. ocommons-logging-1.0.2
6. ocommons-logging-api-1.0.2
7. oitext-1.01
Structure of jrxml file
•Report designs.
•Parameters.
•Data Source.
•Fields.
•Expressions.
•Variables
•Report sections
•Groups
•Font and Unicode Support
•Sub reports.
•I18n support
JasperReport – Parameters
•Parameters are object references that are passed-in to the report filling operations.
•Parameters are useful for passing to the report engine that it can not normally find in its
data sourcePublish Post
•Declaring a parameter in report design require specifying name and class :
•There are also the built-in system parameters :
1. vREPORT_PARAMETERS_MAP
2. vREPORT_CONNECTION
3. vREPORT_DATA_SOURCE
4. vREPORT_LOCALE
•Jasper Reports support various types of data source using special interface called
JRDataSource
•
•There is default implementation of this interface (JRResultSetDataSource class ) that
wraps a Result object . It allows the use of any RDMS database through JDBC.
•
• When using a JDBC data source, one could pass a Connection object to the report
filling operations and specify the query in the report definition itself with the XML
definition in element:
–
JasperReport – Fields
•Report fields present the only way to map data from data source into the report
generating routines.
•For example :
–The table has following structure :
JasperReport – Expressions
•Expression can be used for declaring report variables that performs various calculations,
for data grouping on the report , to specify report text fields content
•In an XML report design there are several elements in define expressions :
•, , , , and .
•Example
JasperReport – Variables
•Report variables is special objects build on of top expression and be used simplify the
report design by declaring an expression.
•Variable can perform built-in types of calculations on their corresponding expression
values like : count, sum, average, lowest , highest ….
•Example : $F{Quantity}
1. PAGE_NUMBER.
2. COLUMN_NUMBER.
3. REPORT_COUNT.
4. PAGE_COUNT.
5. COLUMN_COUNT.
6. GROUPNAME_COUNT.
•Subreports are an import feature for a report-generating tool. They allow the creation of
more complex reports and simplify the design work.
• The subreports are very useful when creating master-detail type of reports.
•With the i18n support in the jasper report , associating a java.util.ResourceBundle and
with the value REPORT_LOCALE.
Advantages
•Handle complex reports, subreports with highly complex layouts, pixel-perfect page-
oriented output for the Web or print, and crosstabbing, and output reports in PDF, XML,
HTML, CSV, XLS, RTF, or TXT.
•Create integrated charting with comprehensive chart types.
•Use multilanguage Unicode and other native encodings, dynamic text localization, and
localized date, number, and currency formatting. •Scale with high-performance report
generation with no limit to report size. •Access data flexibly with JasperReport’s built-in
support for JDBC, EJB, POJO, Hibernate, XML, and more.
•Extensive formatting capabilities
Disadvantages
Tools
•i-Reports
•Jasper Assistant for Eclipse
iReport
ØiReport is a visual tool to obtain XML files for JasperReports.
ØIt provides a WYSIWYG environment to design reports .
ØiReport is openSource program that can create complex reports which can use every
kind of Java application through JasperReport library and written 100% pure Java.
Features
Ø99% support of JasperReport XML tags.
ØWYSIWYG editor for creation reports.
ØSupport for all database accessible by JDBC.
ØIntegrated report compiler and exporter.
ØVirtual support for all kinds of Data Sources.
ØSupport sub reports.
ØSupport for document templates
ØManagement of a library of a standard objects (number of pages ,..).
ØDrag’n drop
ØSupport for unicode and non latin languag
Demo
•Project Structure
• 3. Create a folder called report under WEB-INF place all “.jrxml” files
• 4. Create a HelloWorld.jrxml
HelloWorld.jrxml
HelloWorld.java (Servlet)
try {
jasperReport =JasperCompileManager.compileReport(
context.getRealPath(“
/WEB-INF/report/MinCommissionBankReport.jrxml"));
jasperPrint = JasperFillManager.fillReport(jasperReport,
parameter, ConnectionDb.getConnection() );
// To generate HTML output
createReport(request,response,jasperPrint );
} catch (Exception e) {
e.printStackTrace();}
}
private void createReport(..)throws ServletException, IOException{
JRHtmlExporter exporter = null;
try {
response.setContentType("text/html");
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.
IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());
exporter.exportReport();
} catch (RuntimeException e) {throw e; }
catch (Exception e) {
System.out.println("1"+e);
}
}
After two full days work, I managed to create a web interface for jasper reports. Now I
can open a .pdf file in the browser created using any .jrxml file.
The procedure to open a .pdf in a web browser created using .jrxml file :
Note: I'm using postgresql and created a database called "jasper" by giving the
username "postgres" and the password "root".
Database driver: org.postgresql.Driver
Location: jdbc:postgresql://localhost:5432/jasper","postgres","root"
1. Open netbeans and create a new project "WebApplication". When you are creating the
new project select your server. Here I am using Tomcat 5.5 .
* commons-beanutils-1.8.0.jar
* commons-collections-3.2.1.jar
* commons-digester-1.7.jar
* commons-logging-1.1.jar
* groovy-all-1.5.5.jar
* iText-2.1.0.jar
* spring.jar
* jasperreports-3.6.2.jar
* postgresql-8.3.-603.jdbc3.jar (This is for postgresql users only. Select this .jar file
according to the database you are using.)
If you have installed iReports-3.6.2 in the default path you could find those files in
"C:\Program Files\Jaspersoft\iReport-3.6.2\ireport\modules\ext"
3.Create a new Servlet as "Jasper" and copy and paste these code there.
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
public class Jasper extends HttpServlet
{
Connection conn=null;
ServletOutputStream servletOutputStream = response.getOutputStream();
try{
Class.forName("org.postgresql.Driver");
System.out.println("I am done");
conn=
DriverManager.getConnection("jdbc:postgresql://localhost:5432/jasper","postgres","root"
);
System.out.print("connected");
jasperReport =
JasperCompileManager.compileReport(getServletConfig().getServletContext().getRealPa
th("/reports/jasperreport_demo.jrxml"));
try
{
bytes = JasperRunManager.runReportToPdf(jasperReport,new HashMap(), conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
}
catch (JRException e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
4. Build your web project. Don't worry about the errors you get ;)
5.Go to the folder where your netbeans web project is and go to the web folder inside the
build folder(..\WebApplication\build\web) and create a new folder called "reports".
6. Copy your "jasperreport_demo.jrxml" file from the place where you have created it
and paste it inside the "reports" folder you just create.
7. Build your project and Run the "Jasper.java" servlet created using the above code.
The .pdf file will be display in your web browser with the data you retrieved from the
database.