Introduction: –
This document explains how to merge multiple JasperReports into a single PDF or another output format using Java.
The following technologies have been used to merge multiple JasperReports into a single PDF or another output format,
- Java
- Apache Tomcat (Application Server)
Why we need to do: –
The following technologies are used to merge multiple Jasper Reports into a single PDF or other formats using Java.
How do we solve:
Step 1 : If there is a requirement to merge multiple JasperReports into a single PDF or other format, this approach can be used to achieve it without relying on external libraries or plugins.
Code to Merge Output from Multiple JasperReports
Code:
<%@ page import=”net.sf.jasperreports.engine.*” %>
<%@ page import=”net.sf.jasperreports.engine.export.*” %>
<%@ page import=”java.sql.*” %>
<%@ page import=”java.util.*” %>
<%@ page import=”java.io.*” %>
<%@ page import=”javax.naming.*” %>
<%@ page import=”javax.sql.*” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + “://” + request.getServerName() + “:” + request.getServerPort() + path + “/”;
%>
<!DOCTYPE HTML >
<html>
<head>
<base href=”<%=basePath%>”>
</head>
<body>
<%
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
HashMap<String, Object> jasperParameter = new HashMap<String, Object>();
Properties prop = new Properties();
Properties env = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
Process p = null;
String OS = System.getProperty(“os.name”).toLowerCase();
try {
if (OS.contains(“windows”)) {
p = r.exec(“cmd.exe /c set”);
} else {
p = r.exec(“/bin/env”);
}
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
int idx = line.indexOf(‘=’);
if (idx < 0) continue;
envVars.setProperty(line.substring(0, idx), line.substring(idx + 1));
}
if (env == null) env = envVars;
String envpath = env.getProperty(“APEX_SCHEMA_PATH”);
String dir = envpath + “schema_properties.properties”;
prop.load(new FileInputStream(dir));
String schema = request.getParameter(“schema”);
String pdfnames = request.getParameter(“pdfname”);
String secondPdfName = request.getParameter(“pdfname2”);
String subreportname = request.getParameter(“subname”);
String server = prop.getProperty(schema + “.server”);
String port = prop.getProperty(schema + “.port”);
String sid = prop.getProperty(schema + “.sid”);
String username = prop.getProperty(schema + “.username”);
String password = prop.getProperty(schema + “.password”);
String jrxmlloc = prop.getProperty(schema + “.jrxmlloc”);
String url = “jdbc:oracle:thin:@” + server + “:” + port + “:” + sid;
Connection con = DriverManager.getConnection(url, username, password);
for (int i = 1; i <= 10; i++) {
String paramValue = request.getParameter(“p” + i);
if (paramValue != null) {
jasperParameter.put(“parameter” + i, paramValue);
}
}
if (subreportname != null && !subreportname.trim().isEmpty()) {
jasperParameter.put(“SUBREPORT_DIR”, jrxmlloc);
}
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlloc + pdfnames + “.jrxml”);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, con);
jasperPrintList.add(jasperPrint);
if (secondPdfName != null && !secondPdfName.trim().isEmpty()) {
JasperReport secondReport = JasperCompileManager.compileReport(jrxmlloc + secondPdfName + “.jrxml”);
JasperPrint secondPrint = JasperFillManager.fillReport(secondReport, jasperParameter, con);
jasperPrintList.add(secondPrint);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();
ByteArrayInputStream inputStreamForStruts2 = new ByteArrayInputStream(outputStream.toByteArray());
response.setContentType(“application/pdf”);
response.setContentLength(outputStream.size());
ServletOutputStream outStream = response.getOutputStream();
outputStream.writeTo(outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace(new PrintWriter(out));
}
%>
</body>
</html>
Step 2: Save this code snippet as .jsp file(test.jsp) and upload into Java server like Tomcat and we can call it from the front end.
URL to be framed:
https:/IP:Port/test.jsp?m1=&schema=value&pdfname=report1&pdfname2=report2&subname=&p1=value&p2=value&p3=value
Jasper template nameà&pdfname(report1.jasper file)–1st jrxml
Jasper template nameà&pdfname(report2.jasper file)–2nd jrxml
Both will compile and print as single pdf
Conclusion:
You can efficiently merge multiple JasperReports into a single PDF or other formats using standard JasperReports APIs with a JSP on an application server like Apache Tomcat. This approach integrates easily with Oracle APEX, streamlines report handling, and delivers a unified, user-friendly output without external libraries or plugins.