How to display a Jasper Report inside a HTML page - java

So, I already have the report made, but the problem is that I'm using GetMapping to display, and it opens on another page alone, but I need it to open on a div, a table, a card, or even a modal. It just can't change from the previous page.
I have no idea how to open it with out redirecting the page. Any suggestion is appreciated
Method on the controller
#GetMapping("/seguro")
public void export(HttpServletResponse response) throws IOException, JRException, SQLException {
response.setContentType("text/html");
JasperPrint jasperPrint = null;
jasperPrint = seguroReportService.generatePromissoria(1L);
HtmlExporter htmlExporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
htmlExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
htmlExporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
htmlExporter.exportReport();
}
Method to get the report file
public JasperPrint generatePromissoria(Long id) throws SQLException, JRException, IOException {
Connection conn = jdbcTemplate.getDataSource().getConnection();
String path = resourceLoader.getResource("classpath:/reports/SeguroReport.jrxml").getURI().getPath();
JasperReport jasperReport = JasperCompileManager.compileReport(path);
// Parameters for report
Map<String, Object> parameters = new HashMap<>();
parameters.put("titulo", "Relatório de Seguros");
parameters.put("ID", id);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);
return print;
}
page where is supposed to open the report
Report opening in another page

I resolve the problem using the Iframe, but instead of passing the HTML on the src="", I made a onClick function on the button from my form targeting the name of the iframe.
<iframe name="my_iframe" src=""></iframe>
<button onclick="this.form.target='my_iframe'">My Button</button>

Related

Jasper Report - How to change xpath executer factory

I was checking the following question JasperReports fillReport too slow and resource consuming and I tried to apply the verfied answer but couldn't notice any change in the generation of my reports.
Basicaly what I'm trying to do is to change the xpath executer factory for JasperFillManager to use Jaxen instead of Xalan but I can't seem to know where I should place the following line in my code
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
"net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
My code block looks as follows
private JasperPrint getJasperPrintInstance(JasperReport report) throws JRException, NamingException, SQLException {
JasperPrint jasperPrint = null;
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
"net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
if (dataSource == null) {
jasperPrint = JasperFillManager.fillReport(report, this.reportParams, connection);
} else {
jasperPrint =
JasperFillManager.fillReport(report, this.reportParams,
new JRBeanCollectionDataSource(getDataSource()));
}
return jasperPrint;
}
Even when I change "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory" to anything else nothing changes.
Can you please let me know what I may be doing wrong?
This happens because you are not using the newly created context. You need to create a JasperFillManager instance with it:
JasperFillManager jasperFillManager = JasperFillManager.getInstance(context);
and use that with its instance methods instead of the class ones like so:
jasperPrint = jasperFillManager.fill(report, this.reportParams, connection);

How to print Jasper reports from servlets?

I am trying to print a Jasper report from my web application's servlet. Below is the code which calls the printing function.
//Print the report
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("companyName",userBean.getCompanyName().trim());
params.put("companyPhone",userBean.getPhone().trim());
params.put("patientName",demographicsBean.get(0).getfName()+" "+demographicsBean.get(0).getmName()+" "+demographicsBean.get(0).getlName());
params.put("patientAge",String.valueOf(demographicsBean.get(0).getDob()));
params.put("address",demographicsBean.get(0).getAdrLine1()+" "+demographicsBean.get(0).getAdrLine2());
java.util.Date date = new java.util.Date();
params.put("date",String.valueOf(new Date(Calendar.getInstance().getTimeInMillis())));
params.put("doctorName",subUserBean.getFirstName()+" "+subUserBean.getLastName());
params.put("drugName",drug);
params.put("ptInstructions",ptInstructionsTxt);
params.put("quantity",request.getParameter("quantityTxt"));
params.put("refills",refilTxt);
Connection connection = DBMaster.getInstance().getConnection();
ReportConnector r = new ReportImpl();
r.printReport(getClass().getResourceAsStream("/ReportResources/Prescription2.jasper"), params,connection);
Below is the code printing the report.
public void printReport(InputStream path, Map<String, Object> params, Connection con) throws JRException
{
JasperPrint jasperPrint = JasperFillManager.fillReport(path, params, con);
JasperPrintManager.printReport(jasperPrint, false);
}
The issue is, eventhough this worked in localhost, nothing is getting printed (I do not have a printer now, but MS One Note should be open as the printer. That is happening in localhost).
what is wrong here?

How to display the reports on client side jsp?

I am trying to display the result in jsp page. In my In dex.html has a link as ,
Vehicle Report
In vehicleReport.jsp I am displaying report as,
<body>
<%
Connection con=null;
try{
con=VehicleDetails.getConnection();
InputStream inputStream = new FileInputStream ("E:/ReportFld/report4.jrxml");
Map parameters = new HashMap();
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
#SuppressWarnings("unchecked")
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
out.clear(); // where out is a JspWriter
out = pageContext.pushBody();
JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
}
catch(Exception e){e.printStackTrace();}
finally{
con.close();
}
%>
</body>
But problem is report is not visible. In jsp it displays some unreadable text. and I am getting error message as,
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Reports] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:636)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
How to resolve this?
Try using
JasperExportManager.exportReportToPdfFile(JasperPrint jsperPring, String fileName);
instead of
JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
and then display your pdf in browser or anywhere you want.

How to show the content of a List in JasperReports

I have a Collection of type List that I want to pass it to a report using JasperReports and iReport on NetBeans, but I can't manage to show the content of the list on the Report.
I don't know how to show the data in the Details section of the jrxml. What parameter should I use?
The list has Objects of type Proveedor (Provider in Spanish) and has variables like name, code, etc.
Here is the code of the method who call to JasperReports:
public void generateReport(List<Proveedor> aList){
String reportName = System.getProperty("user.dir") + "\\src\\reports\\lista-proveedores.jrxml";
JRBeanCollectionDataSource dataSource;
JasperReport jasperReport;
JasperPrint jasperPrint;
try{
//1-I fill the dataSource with the Collection
dataSource = new JRBeanCollectionDataSource(aList);
//2-Compile the XML
jasperReport = JasperCompileManager.compileReport(reportName);
//3-Fill the report with the datasource
jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
//4-Export to PDF and save to disk
JasperExportManager.exportReportToPdfFile(jasperPrint, "Proveedores.pdf");
System.out.println("Finished!");
}catch (Exception e){
System.out.println(e);
//e.printStackTrace();
}
}

pdf download dialog window not appear in browser. extjs 4 with java

I am able to generate a report in pdf form using JasperReports and Java.
The generated report is not available to be downloaded to the client side.
I am generating pdf file using the code below:
public void getTaskreportPDF(Session openSession,HttpServletRequest request,HttpServletResponse response) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/contact","root","root");
Map params = getParameters(openSession);
Date date = new Date();
String reportfileName = "report"+date.getDate()+"-"+date.getMonth()+"-"+date.getYear()+"-"+date.getTime()+".pdf";
JasperDesign jasperDesign = JRXmlLoader.load(this.getClass().getResourceAsStream("/com/gantt/report/ganttreport.jrxml"));
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperprint = JasperFillManager.fillReport(jasperReport, params,con);
JRAbstractExporter exporterPDF = new JRPdfExporter();
exporterPDF.setParameter(JRExporterParameter.JASPER_PRINT, jasperprint);
exporterPDF.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
response.setHeader("Content-Disposition", "inline;filename="+ reportfileName);
response.setContentType("application/pdf");
exporterPDF.exportReport();
} catch(Exception exception) {
System.out.println("Error occured " +exception.getMessage());
}
}
My firebug net tab shows that I had got the pdf report file of 4 kb as response. But the problem is that download window not appear, so I cannot save it or view that report.
My firebug shows:
Content-Disposition inline;filename=report21-0-112-1327135412907.pdf
Content-Type application/pdf
What mistake I am making which makes my download window not to appear?
Use attachment instead of inline for the Content-Disposition header.

Categories