How to print Jasper reports from servlets? - java

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?

Related

How to display a Jasper Report inside a HTML page

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>

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

JRBeanCollectionDataSource in jasper how to use

try {
//Connecting to the MySQL database
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital", "root" ,"root");
String report="C:\\Users\\DELL\\report10.jrxml";
JasperReport jasperreport=JasperCompileManager.compileReport(report);
patientDB DataBeanList = new patientDB();
ArrayList<patientBean> dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map<String,Object> parameters = new HashMap<String,Object>();
JasperPrint jp=JasperFillManager.fillReport(jasperreport, parameters,beanColDataSource);
JasperViewer.viewReport(jp);
}
catch(Exception e)
{
out.println(e);
}
i am new in jasper report trying to get value from bean class but nothing happen i spend my last 7 days for this
Please clarify,
What is the error you are getting?
does the DataBeanList.getDataBeanList() return objects with values?
Is Db connection configuration is correct?
the path to jrxml is correct?(I think you don't need double slash in path)

My Jasper file wont work

I'm trying to make this program but i cannot take a report out of it... even if it worked in java netbeans when i tried to run it using the executable JAR file i get this error !
my coding part is
JRTableModelDataSource datasource = new JRTableModelDataSource(dt.getModel());
String reportSource = "file/door_reports.JRXML";
Map<String, Object> params = new HashMap<>();
params.put("c1", subtot.getText());
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperViewer.viewReport(jasperPrint1, false);
error is : error compiling java source file D:/test/dist/Blank_A4_1426091490655_11644
above file do not even exist! anybody know what would be the reason?
Use this method, it should be useful for you:
public static void GenerateReport(String filename, HashMap parameter) {
try {
JasperDesign jspDesign = JRXmlLoader.load(filename);
JasperReport jspReport = JasperCompileManager.compileReport(jspDesign);
JasperPrint jspPrint = JasperFillManager.fillReport(jspReport, parameter, yourconnection);
JasperViewer.viewReport(jspPrint, false);
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e);
}
}
and be sure of the path of the file, good luck.
Did you rename your jxml after you edited it in Jasper?
The report sets the report file name you chose while creating it in Jasper as name, so if you did simple rename your file and not change the name directly in the jxml too... i think there could be your problem.

JasperReports Server and Swing - How to access repository/report from server

I'm looking for a solution how to fill a report that is not local on my computer, but on a JasperReports Server.
Is there any example for getting a connection to a Repository or a Report on the Jasperserver?
it should be something like,Pseudo-code:
Map<String,Object> jasperParameter = new HashMap<String,Object>();
String linkToMyRepository="localhost:8080/jasperserver/repository"
RepositoryService service = new RepositoryService("linkToMyRepository");
JasperReport report = (JasperReport)service.getResource("myreport");
JasperFillManager.fillReport(report, jasperParameter, con);
This is only pseudo-code, can someone help me out?
Thanks alot!
solved!
JServer server = new JServer();
server.setUrl("http://localhost:8080/jasperserver/services/repository");
server.setUsername("username");
server.setPassword("password");
WSClient client = new WSClient(server);
ResourceDescriptor resourceDescriptor = new ResourceDescriptor();
resourceDescriptor.setUriString ("/reports/myreportname");
Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("MY_PARAMETER_NAME", "myparametervalue");
JasperPrint printer = client.runReport(resourceDescriptor, parameterMap);
JasperViewer.viewReport(printer, false, Locale.GERMAN);

Categories