How to create jrxml for sub reporting - jasper-reports

I want create a complex jrxml file to create PDF.
I want to put another jrxml in my jrxml file.
So.. my question is how to put one jrxml to another jrxml
My jrxml files are: salesreport.jrxml, financialreport.jrxml and report.jrxml
I want to put salesreport.jrxml and financialreport.jrxml to report.jrxml.

Usually you develop reports with iReport.
iReport has facilities for including subreports within reports (by dragging the subreport icon onto the master report page).
Note that JasperReports uses absolute paths for file references. I would recommend you set up parameters to your reports as follows:
$P{ROOT_DIR}
$P{SUBREPORT_DIR}
Give $P{SUBREPORT_DIR} a default value of $P{ROOT_DIR} + "subreports/".
At that point, you can pass the absolute path as $P{ROOT_DIR} into your report and then the subreports will be stored in a subdirectory called subreports, which is located in $P{ROOT_DIR}.

Or use the JasperCompileManager class from within the master jrxml file to dynamically compile it.
<subreportExpression><![CDATA[JasperCompileManager.compileReport($P{SUBREPORT_DIR}+"/myFile.jrxml")]]></subreportExpression>

Related

How does JasperReports engine read subreport path?

I would like to know how report read the path of subreport, because I put the main and sub report in the same folder and I put:
<subreportExpression><![CDATA["subReportname.jasper"]]></subreportExpression>
and it doesn't work but when I put the absolute path it works effeciently.

Jaspersoft subreport with source code

I have my reports stored into a database as the source code. I need to then use this source code in my report as a subreport.
When I give the subreport expression the source code, it thinks it is a path to the file.
Is there a way to do this without needing to store the source code as a .japser file?
According to documentation subreportExpression can return a JasperReport object too.

Print application version for each report using JasperReports

We are using JasperReports 6.0.0 and have about 30 templates for different reports that have almost nothing in common. Today we have got a requirement to print application version at the bottom of each page for every report.
I could not find any good solution to do it without modifying every template file. Does JasperReport allows to specify general layout for all templates? Is there any solution to add label to each page without modifying existent templates?
Some approaches:
Modify all .jrxml files and add a footer to each one, manually.
Use XSLT to modify all the .jrxml templates and add a footer.
Use iText to modify the report output after generating the report PDF.
Use the JasperReports API to programmatically inject a footer band into the in-memory template prior to compilation and execution.
Use the JasperReports API to programmatically inject a footer band into each .jrxml file and save the file back to disk.
Create a master template and include a parameterized subreport.
The last approach would have a master template as follows:
-----------
| Title |
-----------
| Subreport |
-----------
| Footer |
-----------
Then, when running the report, pass in a parameter that indicates what subreport to include (i.e., one of thirty). In this fashion, the Title Band and Footer Band are shared by all thirty reports. It's more work, but ultimately the most flexible solution.
If the Title Band differs completely across all reports, parameterize it to use a subreport.
Here's a screenshot of a master report that includes a subreport, which is given as a parameter. In the screenshot, the Title Band doesn't include a subreport because each of the several reports looks identical (the human-readable report title is also a parameter). It should be easy to see that if the Title Band had to change its layout for each report, then using a subreport instead would facilitate such a solution:
The footer, though, as shown in the screenshot, is one possible answer to your question.
You may need to define a variable that allows for calculating the page numbers:
<variable name="V_CURRENT_PAGE_NUMBER" class="java.lang.Integer" resetType="Page">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[$V{PAGE_NUMBER}+1]]></initialValueExpression>
</variable>
And a corresponding text field that references the page number:
msg("Page {0} of {1}", $V{V_CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})
Keeping in mind that JasperReports uses absolute paths, define a few parameters to avoid hard-coding the location of the subreports. For example:
$P{P_REPORT_SUBREPORTS_PATH} + $P{P_SUBREPORT_NAME} + ".jasper"
Here, P_REPORT_SUBREPORTS_PATH is defined in terms of a P_REPORT_BASE variable:
$P{P_REPORT_BASE} + "subreports/"
This allows the report IDE to use a different directory than that of the web server by providing a different value for P_REPORT_BASE, depending on context.
Finally, keep in mind that what might be seemingly different reports could be a parameterized report. JasperReports allows for custom styles, which makes possible to dynamically change the report appearance (e.g., text justification, fonts, emphasis, borders, and so forth). In the screenshot, the report title, for example, is changed with each of the several reports--but they all share the same Title Band because the report title is passed in as a String parameter.

How to embed JasperReport sub-report into a jar

I have a jasper (master) report embed in a jar. The report is got from a java input stream. The jasper report is made by iReport 3.7.0
In this master report, there are a few sub-reports. At the moment I have to use
Subreport Expression : $P{SUBREPORT_DIR} + "\\SubReport.jasper"
Expression Class : java.lang.String
in the iReport to make it works. It means that I store the subReport.jasper in SUBREPORT_DIR folder.
My question is, how to embed these sub-reports in the jar like the master report? And what should fill in the "Default Value Expression"? (if it is needed)
The closest question and answer so far I have seen is in here
How to load subreport resources with Jasper?
However the answer is not sufficient to me for master report compile. There is an error message saying missing sub-report when compile (and this is why I think the Default Value Expression may be needed?)
Thank you very much!
for example:
i have report and subreport, located in some *.jar file. to invoke subreport i set subreport expression
getClass().getResource("/path/to/my/report/in/a/jar/subreport.jasper").openStream()
and choose subreport expression class java.io.InputStream
it is working!
I've described one possible solution in an answer in another question. Question is not identical, but the solution solves this problem also.
That solution uses spring, but replacing spring specific parts (FileSystemResourceLoader) with Class.getResourceAsStream() should not be too big of a problem.

How include subreport in a parent directory into a report in iReport?

How can I include a subreport (.jrxml) file which is located on the parent folder of a specified report in that report? Seems I can't use relative path for specifying subreport (It failed to compile when I want to use relative path like ../parent/subreport.jrxml).