I want to know do JasperServer accepts xml file as the report data source and whether we can perform same operation on xml nodes that we perform on the various data-source in adhoc report creator like dragging dropping etc.?
yes, jasper does support XML based datasources, you can create your report just the way you would create it using any other datasource. Jasper has an abstraction from datasources which you provide to ensure it behaves the same (or atleast similarly) with different datasources.
look here and here for some additional info
Here is the sample Code I tested for xml file as data source and out is PDF report:
JasperReport jasperReport = JasperCompileManager.compileReport("<Path of jrxml file>");
JRXmlDataSource xmlDataSource = new JRXmlDataSource("<Path of xml file>", "XPath Query");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), xmlDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "<Path of file PDF output file>");
Related
I have a Spring project. I am also using JasperReport to build report templates for my Spring project to call, use and generate.
After looking through a couple of tutorials, I finally managed to get my Spring project to call and generate my report template.
But one thing I'm not familiar with is that I've configured my Jasper Reports with a dataset query which populates the template with data when I preview it within JasperSoft. But when I call this same report.jasper file through Spring, all I got was an empty report(albeit with some expected formatting)
Most of the examples Ive come across has their JasperReport use an empty datasource while they passed in the required data from the db through Spring. But my report is making use of multiple dataset queries (and thus using multiple subtables), so I'm not sure where do I go from here; whether I need to manually pass in the data through Spring like those examples or can I depend on the JasperReport to do so.
I'm still relatively new to both Jasper and Spring.
Created based on this link, this is a sample of my test function:
#GetMapping(path = "/report")
void testFunction2(HttpServletResponse response) throws IOException, JRException {
String sourceFileName = new File("C:\\\\Users\\\\User\\\\JaspersoftWorkspace\\\\MyReports\\\\base3.jasper").getAbsolutePath();
JRBeanCollectionDataSource sampleDataSource = new JRBeanCollectionDataSource(null);
String testName = "testName.xlsx";
HashMap<String, Object> theHashMap = new HashMap<>();
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, theHashMap);
JRXlsxExporter exporter = new JRXlsxExporter();
SimpleXlsxReportConfiguration reportConfigXLS = new SimpleXlsxReportConfiguration();
reportConfigXLS.setSheetNames(new String[] { "sheet1" });
exporter.setConfiguration(reportConfigXLS);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
response.setHeader("Content-Disposition", "attachment;filename="+testName);
response.setContentType("application/octet-stream");
exporter.exportReport();
}
I think I solved my issue/confusion.
The most important thing I needed was a connection to my DB. Most of the tutorials has their datasource connected from within the Spring project itself, make a repository call and send the data from the call to the report to be generated.
In my case, I'm not calling the DB from the Spring side, but from the Jasper side.
So, alternatively, and the solution to my issue, is that I could just send the connection details using DriverManager.getConnection() and parsing this as the 3rd variable to fillReport as opposed to the datasource.
Basically, what everyone else is doing
JRBeanCollectionDataSource sampleDataSource = new JRBeanCollectionDataSource(repositoryService.getData());
JasperPrint report = JasperFillManager.fillReport(compiledReport, parameters, sampleDataSource);
But what I want is this
Connection con = DriverManager.getConnection("jdbc:postgresql://url:port/dbName","dbUsername","dbPassword");
JasperPrint jasperPrint = JasperFillManager.fillReport(compiledReport, parameters, con);
(but both methods are correct)
Once I did that 2nd method, my report data is successfully generated using all the pre-configured SQL queries within JasperSoft.
I got the idea from here
This question already has answers here:
Blank PDF even with the simplest Jasperreport jrxml
(3 answers)
Jasper report exports empty data in PDF format when there is more data
(5 answers)
Closed 4 years ago.
Using a Jaspersoft Studio 6.6.0 I created a very simple report with a static text and a red rectangle (as well as many other things I tested before). There is no query to fetch data, no dynamic field or anything.
This is what the design looks like:
Preview looks fine in all different formats (HTML, PDF etc..)
The problem I have comes when I try to generate the report in my web app. Using a library version 6.7.0.
HTML Export coded like this yields an empty String
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
StringBuilder htmlStringBuilder = new StringBuilder();
HtmlExporter exporter = new HtmlExporter();
SimpleHtmlExporterConfiguration conf = new SimpleHtmlExporterConfiguration();
conf.setHtmlHeader("");
conf.setBetweenPagesHtml("");
conf.setHtmlFooter("");
conf.setFlushOutput(true);
exporter.setConfiguration(conf);
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(htmlStringBuilder);
exporter.setExporterOutput(output);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.exportReport();
return htmlStringBuilder.toString(); // empty string
Eport to PDF using creates a single page PDF file. The page is blank.
JasperExportManager.exportReportToPdfFile(jasperPrint, "file.pdf");
I tried to compile the JRXML file using both the Studio and also in the Java code. The result is still the same. JasperPrint object seem to be populated, however no elements are present on the page, which I reckon is related. Any chance to find out why?
I see two options for this:
In your java code import the net.sf.jasperreports.engine.JREmptyDataSource class and fill the jasperPrint like so:
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
This should guarantee that your detail band is displayed once. This is what JasperSoft Studio uses when you preview a report with One Empty Record Data Adapter.
In JasperSoft Studio, move all the content out of the Detail band to Summary band, for example, and in Report Properties tab select:
When No Data Type: All Sections No Detail
This way all the other bands, except the Detail one, will be rendered when you don't provide any data.
Using jasper-reports 5.6.1 I am able to generate the reports in pdf format, but I am not able figure out how to generate a .doc format by using jasper.
byte[] exportReportToPdf = JasperExportManager.exportReportToPdf(print);
is for generating a pdf format file like this is there any similar view class for doc format?
Try like this
JasperPrint jasperPrint = JasperFillManager.fillReport("myReport.jasper", reportParameters, dataSource);
Exporter exporter = new JRDocxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
File exportReportFile = new File("D:\\Temp\\report.docx");
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(exportReportFile));
exporter.exportReport();
HTH
Before executing a report, the JRXML must be compiled in a binary object called a Jasper file(*.jasper). This compilation is done for performance reasons. Jasper files are what you need to ship with your application in order to run the reports. Once the report is compiled it is filled with data from the application. The class net.sf.jasperreports.engine.JasperFillManager provides necessary functions to fill the data in the reports.
The report execution is performed by passing a Jasper file and a data source to JasperReports. There are plenty of types of data sources, it's possible to fill a Jasper file from an SQL query, an XML file, a csv file, an HQL (Hibernate Query Language) query, a collection of Java Beans, etc... If you don't find a suitable data source, JasperReports is very flexible and allows you to write your own custom data source.
JasperFillManager.fillReportToFile( "MasterReport.jasper" , parameters, getDataSource());
This operation creates a Jasper print file (*.jrprint), which used to either print or export the report.
- See more at: http://blog.manupk.com/2012/11/using-jasper-reports-to-create-reports.html#sthash.rFqV8K4i.dpuf
I'm loading the jasper reports file from .jasper file to improve the performance as below.
JasperReport rpt = (JasperReport)JRLoader.loadObjectFromFile(location);
but I have a requirement now, that a query which is formed dynamically has to be set to .jasper (compiled) file. is there a way to do the same?
Something like this?
Java
JasperReport rpt = (JasperReport)JRLoader.loadObject(Your .jasper file path);
Map paramMap = new HashMap();
paramMap.put("p_sql", "select * from baztable where foo='bar' and fuga='hoge' order by username");
// pass the dynamic query to .jasper file.
JasperPrint print = JasperFillManager.fillReport(rpt, paramMap, connection);
jrxml
<parameter name="p_sql" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["select * from baztable order by username"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[$P!{p_sql}]]>
</queryString>
You can use the $P! (Parameter with exclamation) which can be used for conditional queries.
Using this you can craft dynamic queries based on certain conditions.
A guide for the same can be found here.
Just open .jasper file in to ireport so you can load .jasper file and edit it.
This is a hack method for the JRXML file. The JRXML file has your query. You can write a Java code or any other code to replace/write down your Query here:
Sample MongoDB Query:
<queryString language="MongoDbQuery">
{'collectionName':'customer'}
</queryString>`
I am developping a Spring MVC - Angularjs application.
I need to print reports, I chose JasperReport to do that.
Before I move on, I want to know if I can generate a report, then directly print it on the default printer set up on the client computer (printer which can change according to users) without displaying it on screen.
I have been looking for answers on this specific need, but couldn't find any.
If anyone knows about it....
Source to generate report and print it:
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("Title", "My Report");
InputStream reportStream = this.getClass().getResourceAsStream(TEMPLATE);
JasperDesign jd = JRXmlLoader.load(reportStream);
JasperReport jr = JasperCompileManager.compileReport(jd);
JasperPrint jp = JasperFillManager.fillReport(jr, params, datasource.getDataSource());
JasperPrintManager.printReport(jp, false);
You can create an object in HTML page which hold PDF & then print it using print() method or use java.awt.print.PrinterJob. See below examples.
Print embedded PDF from browser with Javascript, HTML5, AngularJS
Print a PDF from the browser
Print PDF directly from JavaScript
Print JasperPrint directly to printer ? - Java PrinterJob
Different examples using java.awt.print.PrinterJob
Hope this helps you