iReport Designer can create PDF files for publishing as static web content. Alternatively, the products of iReport Designer -- .jrxml and .jasper files -- can be called by JSP for dynamic configuration, layout, or avoiding the file system.
PDF Generated by JSP |
Simple PDF JSP
The following JSP is a two-line API invocation that configures a JasperPrint object and then streams it using a static method "exportReportToPDFStream".
<%@ page contentType="application/pdf" %>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.util.*" %>
<%
JasperPrint print = JasperFillManager.fillReport(
"C:/Users/Carl2/Documents/iReport/EmptyReport.jasper",
new HashMap(),
new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(print,
response.getOutputStream());
%>
JasperPrint
The JasperPrint object is a ready-to-export internal copy of a report. In this case, a file called EmptyReport.jasper was created in iReport Designer. The .jasper file is the compiled product from iReport Designer. This is different than the .jrxml file that contains the actual contents of a Jasper Report. The compilation comes from either previewing or explicitly compiling a file. The JasperPrint object is filled with substituted parameter values (in contrast to a JasperReport object which is not).
iReport Designer with Compile Command |
In the example above, an empty parameter map ("new HashMap()") is passed to the JasperPrint object as well as an empty JREmptyDataSource. The map and the datasource could be provided by the container (Spring, etc.) or through user input (form, URL). The location and name of the .jasper file should also be parameterized.
JasperExportManager
JasperExportManager is a facade for several classes including JRPdfExporter. JasperExportManager contains a convenient static method exportReportToPdfStream() that takes the JasperPrint object set up in the first line of the scriptlet and the OutputStream from the HTTP response object.
Installation
This example JSP is deployed in Tomcat 7.0.14. The webappp contains the JSP 'streamEmptyReport.jsp' and a /WEB-INF/lib folder. web.xml files are no longer required. The contents of the lib folder are a set o JAR files taken from the modules/ext folder in iReport Designer. For my installation, this is C:\Program Files\Jaspersoft\iReport-4.0.2\ireport\modules\ext.
Be careful not to mix-and-match versions of .jasper files and JAR files. If you get a strange error, something other than "File not found", it's probably an incompatibility. You can't run a 4.0.2-generated .jasper file in a webapp containing 4.0.1 JAR files. A later post will demonstrate compiling on-the-fly which will work with the more portable .jrxml files.
JSP Notes
A Java Server Page (JSP) is a dynamic web page. Usually, JSPs are used to render HTML. In this example, the JSP is used to render a PDF. There is a setting at the top of the example which provides a contentType of application/pdf. This is an important cue to the browser to bring up the Adobe plug-in. Keep the contentType in sync with what's being rendered by the JasperReports API calls.
Publishing static contents generated from iReport Designer is an easy way to get reports online. Having a web server like Apache retrieve static content is very fast and can scale by replicating the static files across multiple instances. However, many cases will require a dynamic component whether a datasource, input parameters, or layout. In these cases use the JasperReports API integrated into a Java EE app or simple JSP.
Hi , i ve used this code to export a crosstab to pdf . But it is shown strange caracters !!!
ReplyDeleteplease could tell me why !!!!!
STRANGE CARACTERS LIKE THIS :
ReplyDelete%PDF-1.4 %���� 4 0 obj <>stream x��]�r�� ��+�J9 ]�{����.�T�M�ڤ�`(Ju I�IJ��� �+�����3��JD� ��� � �1�� ��t������ �q���ȧ.d�:zrz�ۑ�~����}���ݧ��~��v�u���~�����������A.�� ]Hi��r�뜙 �Ց٥�?�����G��|����� �^]\_���Eם^ޜ���>\\u �.?�ߟ�
It looks like the browser is not interpreting the bytes as a PDF. A few things to check or try
Delete1. The contentType is being set to application/pdf. This applies to any calling JSPs that may set contentType in a different file or web servers.
2. Make sure that there is a PDF reader on the calling system.
3. Using a in a for a , map a URL that contains a mime suffix like "/myapp/jasperReport.pdf" to the JSP file.
Good luck
Hello,
ReplyDeleteMaybe you can help me, I am trying to use the JasperExportManager.exportReportToPdf(print, output)
where print =
JasperPrint print = JasperFillManager.fillReport(report, newHashMap<String, Object, ds.getConnection());
and Where output =
FileOutputStream output = new FileOutputStream(new File(**MY FILE LOCAITON"));
It gives me the error:
'exportReportToPdf(JasperPrint, FileOutputStream)' cannot invoke 'exprotReportToPdf(net.sf.jasper...)
How can I fix this.
My overall goal is to use jasper reports to make a pdf which it is doing.
My next goal is to open this file and display the pdf without using the JasperViewer.
Thanks in advance!
Hi,
ReplyDeleteAre you able to replicate the blog example? Create an empty JasperReport and try to invoke it with the JasperPrint and JasperExportManager statements. If that's ok, double-check the datasource. Otherwise, there may be something in the report preventing export (a scriptlet class?).
Sorry but your code not work
ReplyDeleteHi,
DeleteCan you post the error you're receiving?
Hi Carl, first thank you so much for your help,i'am trying to display a report in my jsp page, i created the report and compiled it but when i want to display it in my jsp page i receive the following error:
Delete<%
49:
50: JasperPrint print = JasperFillManager.fillReport(
51: "report3.jrxml",
52: new HashMap(),
53: new JREmptyDataSource());
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
Deletejavax.servlet.ServletException: net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: report3.jrxml
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.index_jsp._jspService(index_jsp.java:109)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
My example is using the compiled .jasper file rather than the source file .jrxml. See section "Jasper Print".
DeleteAlso, make sure that your path is fully-qualified.
I have again the same error, i generated .jasper file with jasper report like you said in this tutorial in section "Jasper Print" and i put it in the main folder of my project finally i got this error:
Deleteorg.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 54
51:
52: <%
53:
54: JasperPrint print = JasperFillManager.fillReport(
55: "monrapport.jasper",
56: new HashMap(),
57: new JREmptyDataSource());
Have you tried fully-qualifying "monrapport.jasper"? Something like C:/Jasper/monrapport.jasper or /home/jasper/monrapport.jasper
Deletei dont have any error,but im not getting any data in the generated pdf
ReplyDeletecode is:-
<%@ page contentType="application/pdf" %>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.util.*" %>
<%
JasperPrint print = JasperFillManager.fillReport(
"C:\\Users\\tech\\Documents\\NetBeansProjects\\jasper\\web\\report1.jasper",
new HashMap(),
new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(print,
response.getOutputStream());
%>
Hi,
DeleteYou're getting the unprocessed JSP code. It sounds like you may have displayed this file in the browser using Open File or a doule-click. Are you running in an app server like Tomcat or JBoss? These will compile the JSP instructions and will invoke the Java commands, returning the bytes of the PDF.
You should put something like http://localhost:8080/jasper-app/streamEmptyReport.jsp
ur datasource is empty
Deleteif u r using oracle
the:
Connection con=DriverManager.getConnection("jdbc:odbc:DSN","username","password");
JasperPrint print = JasperFillManager.fillReport(
"C:\\Users\\tech\\Documents\\NetBeansProjects\\jasper\\web\\report1.jasper",
new HashMap(),
con);
hello
ReplyDeletemy name is alejandro and i am working with jasper report 4.0.5 and jsp with glassfish server, i have tried everything to make this work but nothing make this happen...
the error that the application is
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
root cause
java.lang.NullPointerException
that error is generated when it pass trought
JasperPrint print = JasperFillManager.fillReport(
"C:\\Users\\tech\\Documents\\NetBeansProjects\\jasper\\web\\report1.jasper",
new HashMap(),
con);
my code
<%@page import="net.sf.jasperreports.view.JasperViewer"%>
<%@page import="Utilidades.Conexion"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.design.JasperDesign"%>
<%@page import="net.sf.jasperreports.engine.xml.JRXmlLoader"%>
<%@page import="net.sf.jasperreports.engine.export.*"%>
<%
Conexion con = new Conexion();
Connection conn;
conn = con.conectar();
String id_cartera=(String) session.getAttribute("id_cartera");
Map parameters = new HashMap();
parameters.put("id_cartera", id_cartera);
String archivo = "C:\\Users\\Alejandro\\Documents\\NetBeansProjects\\RecsaCartera\\build\\web\\Reportes\\cartera.jasper";
JasperPrint print = JasperFillManager.fillReport(archivo,parameters,conn);
JasperExportManager.exportReportToPdfStream(print, response.getOutputStream());
%>
i really need help
Hi Alejandro,
DeleteIt looks like your report is expecting an 'id_cartera' parameter. Can you verify that the session is returning a non-null value and that the case of the variable matches what is used for the definition in the report (not "ID_CARTERA" for example)?
hi to all,
ReplyDeletei am swami,i did one jsp page to make report but its showing following error and i already designed jasper report page and stored it in my d: and i am trying to make a report in pdf format by jsp code,i am using IDE is Net beans and i added jasper report-4.0.2 jar in my libraries
this is my jsp page ,
<%@page import="java.sql.*"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.HashMap" %>
<%@page import="java.util.Map" %>
<%
Connection conn=null;
JasperReport jr;
JasperPrint jp;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/guide","root","");
jr = JasperCompileManager.compileReport("D:/03033/I_Report files/Guide_Report.jrxml");
jp = JasperFillManager.fillReport(jr, new HashMap(), new net.sf.jasperreports.engine.JREmptyDataSource());
net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(jp, "D:/Guidereport.pdf");
}catch(Exception e){
out.println(e);
}
finally{
}
%>
ERROR:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.AnnotationProcessor
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:156)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.AnnotationProcessor
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:146)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
Hi,
DeleteThis looks more like a Tomcat problem than a Jasper Reports problem. It's confusing because "Jasper" is the name of both the library that you're using for reporting (Jasper Reports) and the embedded Java Server Pages processing engine, called Jasper. The JasperException is referring to the JSP processor.
Can you verify that a simple JSP processes correctly? Create a file "testme.jsp" that contains an expression "<%= new java.util.Date() %>".
hi .,
ReplyDeletei checked my jsp with this expression <%= new java.util.Date() %> and its running properly ..
i changed my code little bit that its not showing any error only throws some exception and below i mentioned my code
<%@ page contentType="text/html;charset=windows-1252"%>
<%@page import="java.sql.*"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.HashMap" %>
<%@page import="java.util.Map" %>
<%@page import="net.sf.jasperreports.engine.JasperRunManager" %>
Hello World!
<%
Connection conn=null;
Statement st=null;
ResultSet rs=null;
JasperReport jr;
JasperPrint jp;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/guide","root","");
st=conn.createStatement();
rs=st.executeQuery("select * from userentry");
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
//JasperFillManager.fillReportToFile("D:/03033/I_Report files/Guide_Report1.jrxml",new HashMap(),resultSetDataSource);
jr = JasperCompileManager.compileReport("D:/03033/I_Report files/Guide_Report1.jasper");
jp =JasperFillManager.fillReport(jr,new HashMap(),resultSetDataSource);
net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(jp,"D:/Guidereport.pdf");
}catch(Exception e){
out.println(e);
}
%>
Output is:
Hello World!
net.sf.jasperreports.engine.JRException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
i added jasper jar in my libraries
pls check my code and will tell the solutions ...here i am using netbeans 6.5 and database is mysql and reporting tool is I_Report 4.7.0 .....
i am confusing with this lot so may u send any sample code for this ?......
that is need for my project....
Hi,
DeleteI think your error is on the following line
JasperCompileManager.compileReport("D:/03033/I_Report files/Guide_Report1.jasper")
You compile JRXML files and fill .jasper files. The JSP is trying to interpret the binary-based .jasper file as XML and is getting a bad non-character byte in the first position.
Take a generated .jasper file from iReport and fill it (don't compile it) as in the blog's example.
Good luck.
hi ... if u have got the solution pl.z share ..
DeleteGive me example of japser report using jsp..after compile from jsp file it is open in applet..
ReplyDeletesend me full code on. riyadave1@yahoo.com
thanks..
Hi,
DeleteI don't have this example any more. It sounds like there could be a problem with the contentType which is application/pdf. Verify that the page directive is at the top of the jsp and the View Source doesn't show any jsp tags.
Also, a pdf program like Adobe sbould be on the target machine.
hello,i've tried your example and it does not work ,always giving me resource not found ,do i have to do any mappin in the web.xml file but when i display it as html it works.
ReplyDeleteHi,
DeleteAre you having trouble rendering the example in PDF (versus HTML) or with JSPs in general? If the problem might be with a JSP, create a simple non-Jasper example and try to display it.
hi it was just a problem with the itext jar file which wen i build the path went into another folder ,i corrected it and it works fine.
Deletehi, i follow you example and it works but i want to display the generated page in a div it , can you give me some suggestion,thanks
ReplyDeleteRather than using PDF, you should export using HTML. An iframe would work too using the PDF page as a src.
Deletehai to all,
ReplyDeletei am venkatesh i am working on jasper reports.while calling jasper report into the jsp i got error please help me out any one
my error is :net.sf.jasperreports.engine.JRException: Document root element "jasperReport", must match DOCTYPE root "null".
my code is:
<%
JasperReport jr;
JasperPrint jp;
try{
jr = JasperCompileManager.compileReport("C:/Tvd/report21.jrxml");
jp = JasperFillManager.fillReport(jr, new HashMap(), new net.sf.jasperreports.engine.JREmptyDataSource());
net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(jp, "C:/DMS_Report1.txt");
}catch(Exception e){
out.println(e);
}
finally{
}
%>
this source was very useful to me! thank u!!
ReplyDeleteHi Guys,
ReplyDeleteCurrently am having new error during creating a pdf report.
Anyone can help me...
Error viewing report...
java.io.IOException: Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at it.businesslogic.ireport.IReportCompiler.run(IReportCompiler.java:1602) at java.lang.Thread.run(Unknown Source) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 6 more
Is your JSP the same as the JSP listed under "Simple PDF JSP"?
Deletehi i am amol and i am facing problem while calling jasper report from jsp
ReplyDeleteHi All..
ReplyDeleteI'm sampath.
And new to Jasper Reports. I'm using Tomcat 6. while executing, I got this error while exeporting to PDF.
I tried to replace with latest version of itextpdf-5.5.0.jar file.
Could any one respond asap.
Greatly appreciated.
This is my code.
--------------------
JRExporter jrExporter = new JRPdfExporter();
jrExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(signedBy + ".pdf"));
signedBy is the String(for file name)
JRExporter jrExporter = new JRPdfExporter();
This line is giving error.
Error Log:
-------------
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ServiceController threw exception
java.lang.ClassNotFoundException: com.itextpdf.text.pdf.PdfAWriter
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.apache.jsp.raise.eAgreement.eAgreement_005finner_jsp._jspService(eAgreement_005finner_jsp.java:141)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
at org.apache.jsp.raise.eAgreement.eAgreement_jsp._jspService(eAgreement_jsp.java:177)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
at org.apache.jsp.raise.raise_005ftemplate_jsp._jspService(raise_005ftemplate_jsp.java:155)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)